Reorganize node:vm native code

This commit is contained in:
Kai Tamkun
2025-05-05 16:03:51 -07:00
parent a0384f67be
commit c59f7ad61f
10 changed files with 1666 additions and 1507 deletions

View File

@@ -0,0 +1,97 @@
#include "NodeVMSourceTextModule.h"
#include "JSDOMExceptionHandling.h"
namespace Bun {
using namespace NodeVM;
NodeVMSourceTextModule* NodeVMSourceTextModule::create(VM& vm, JSGlobalObject* globalObject, ArgList args)
{
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue identifierValue = args.at(0);
if (!identifierValue.isString()) {
throwArgumentTypeError(*globalObject, scope, 0, "identifier"_s, "Module"_s, "Module"_s, "string"_s);
return nullptr;
}
JSValue contextValue = args.at(1);
if (contextValue.isUndefined()) {
// TODO(@heimskr): should this be `globalObject->globalThis()` instead?
contextValue = globalObject;
} else if (!contextValue.isObject()) {
throwArgumentTypeError(*globalObject, scope, 1, "context"_s, "Module"_s, "Module"_s, "object"_s);
return nullptr;
}
JSValue sourceTextValue = args.at(2);
if (!sourceTextValue.isString()) {
throwArgumentTypeError(*globalObject, scope, 2, "sourceText"_s, "Module"_s, "Module"_s, "string"_s);
return nullptr;
}
JSValue lineOffsetValue = args.at(3);
if (!lineOffsetValue.isUInt32AsAnyInt()) {
throwArgumentTypeError(*globalObject, scope, 3, "lineOffset"_s, "Module"_s, "Module"_s, "number"_s);
return nullptr;
}
JSValue columnOffsetValue = args.at(4);
if (!columnOffsetValue.isUInt32AsAnyInt()) {
throwArgumentTypeError(*globalObject, scope, 4, "columnOffset"_s, "Module"_s, "Module"_s, "number"_s);
return nullptr;
}
JSValue cachedDataValue = args.at(5);
WTF::Vector<uint8_t> cachedData;
if (!cachedDataValue.isUndefined() && !extractCachedData(cachedDataValue, cachedData)) {
throwArgumentTypeError(*globalObject, scope, 5, "cachedData"_s, "Module"_s, "Module"_s, "Buffer, TypedArray, or DataView"_s);
return nullptr;
}
auto* zigGlobalObject = defaultGlobalObject(globalObject);
NodeVMSourceTextModule* ptr = new (NotNull, allocateCell<NodeVMSourceTextModule>(vm)) NodeVMSourceTextModule(vm, zigGlobalObject->NodeVMSourceTextModuleStructure(), identifierValue.toWTFString(globalObject));
ptr->finishCreation(vm);
return ptr;
}
void NodeVMSourceTextModule::destroy(JSCell* cell)
{
static_cast<NodeVMSourceTextModule*>(cell)->NodeVMSourceTextModule::~NodeVMSourceTextModule();
}
EncodedJSValue NodeVMSourceTextModule::link(JSGlobalObject* globalObject, JSArray* specifiers, JSArray* moduleNatives)
{
const unsigned length = specifiers->getArrayLength();
ASSERT(length == moduleNatives->getArrayLength());
if (length == 0) {
return JSC::encodedJSUndefined();
}
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
for (unsigned i = 0; i < length; i++) {
JSValue specifierValue = specifiers->getDirectIndex(globalObject, i);
JSValue moduleNativeValue = moduleNatives->getDirectIndex(globalObject, i);
ASSERT(specifierValue.isString());
ASSERT(moduleNativeValue.isObject());
WTF::String specifier = specifierValue.toWTFString(globalObject);
JSObject* moduleNative = moduleNativeValue.getObject();
m_resolveCache.set(WTFMove(specifier), WriteBarrier<JSObject> { vm, this, moduleNative });
}
return JSC::encodedJSUndefined();
}
JSObject* NodeVMSourceTextModule::createPrototype(VM& vm, JSGlobalObject* globalObject)
{
return NodeVMModulePrototype::create(vm, NodeVMModulePrototype::createStructure(vm, globalObject, globalObject->objectPrototype()));
}
const JSC::ClassInfo NodeVMSourceTextModule::s_info = { "NodeVMSourceTextModule"_s, &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(NodeVMSourceTextModule) };
} // namespace Bun