mirror of
https://github.com/oven-sh/bun
synced 2026-02-11 11:29:02 +00:00
Reorganize node:vm native code
This commit is contained in:
97
src/bun.js/bindings/NodeVMSourceTextModule.cpp
Normal file
97
src/bun.js/bindings/NodeVMSourceTextModule.cpp
Normal 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
|
||||
Reference in New Issue
Block a user