diff options
author | 2023-07-11 19:14:34 -0700 | |
---|---|---|
committer | 2023-07-11 19:14:34 -0700 | |
commit | cbb88672f217a90db1aa1eb29cd92d5d9035b22b (patch) | |
tree | 43a00501f3cde495967e116f0b660777051551f8 /src/bun.js/modules | |
parent | 1f900cff453700b19bca2acadfe26da4468c1282 (diff) | |
parent | 34b0e7a2bbd8bf8097341cdb0075d0908283e834 (diff) | |
download | bun-jarred/esm-conditions.tar.gz bun-jarred/esm-conditions.tar.zst bun-jarred/esm-conditions.zip |
Merge branch 'main' into jarred/esm-conditionsjarred/esm-conditions
Diffstat (limited to 'src/bun.js/modules')
-rw-r--r-- | src/bun.js/modules/BufferModule.h | 141 | ||||
-rw-r--r-- | src/bun.js/modules/NodeModuleModule.cpp | 134 | ||||
-rw-r--r-- | src/bun.js/modules/ProcessModule.h | 39 | ||||
-rw-r--r-- | src/bun.js/modules/StringDecoderModule.h | 11 | ||||
-rw-r--r-- | src/bun.js/modules/TTYModule.h | 13 |
5 files changed, 240 insertions, 98 deletions
diff --git a/src/bun.js/modules/BufferModule.h b/src/bun.js/modules/BufferModule.h index 42eab5173..6e6e39e9c 100644 --- a/src/bun.js/modules/BufferModule.h +++ b/src/bun.js/modules/BufferModule.h @@ -2,11 +2,127 @@ #include "../bindings/ZigGlobalObject.h" #include "JavaScriptCore/JSGlobalObject.h" #include "JavaScriptCore/ObjectConstructor.h" +#include "simdutf.h" namespace Zig { using namespace WebCore; using namespace JSC; +// TODO: Add DOMJIT fast path +JSC_DEFINE_HOST_FUNCTION(jsBufferConstructorFunction_isUtf8, + (JSC::JSGlobalObject * lexicalGlobalObject, + JSC::CallFrame *callframe)) { + auto throwScope = DECLARE_THROW_SCOPE(lexicalGlobalObject->vm()); + + auto buffer = callframe->argument(0); + auto *bufferView = JSC::jsDynamicCast<JSC::JSArrayBufferView *>(buffer); + const char *ptr = nullptr; + size_t byteLength = 0; + if (bufferView) { + if (UNLIKELY(bufferView->isDetached())) { + throwTypeError(lexicalGlobalObject, throwScope, + "ArrayBufferView is detached"_s); + return JSValue::encode({}); + } + + byteLength = bufferView->byteLength(); + + if (byteLength == 0) { + return JSValue::encode(jsBoolean(true)); + } + + ptr = reinterpret_cast<const char *>(bufferView->vector()); + } else if (auto *arrayBuffer = + JSC::jsDynamicCast<JSC::JSArrayBuffer *>(buffer)) { + auto *impl = arrayBuffer->impl(); + + if (!impl) { + return JSValue::encode(jsBoolean(true)); + } + + if (UNLIKELY(impl->isDetached())) { + throwTypeError(lexicalGlobalObject, throwScope, + "ArrayBuffer is detached"_s); + return JSValue::encode({}); + } + + byteLength = impl->byteLength(); + + if (byteLength == 0) { + return JSValue::encode(jsBoolean(true)); + } + + ptr = reinterpret_cast<const char *>(impl->data()); + } else { + throwVMError( + lexicalGlobalObject, throwScope, + createTypeError(lexicalGlobalObject, + "First argument must be an ArrayBufferView"_s)); + return JSValue::encode({}); + } + + RELEASE_AND_RETURN(throwScope, JSValue::encode(jsBoolean( + simdutf::validate_utf8(ptr, byteLength)))); +} + +// TODO: Add DOMJIT fast path +JSC_DEFINE_HOST_FUNCTION(jsBufferConstructorFunction_isAscii, + (JSC::JSGlobalObject * lexicalGlobalObject, + JSC::CallFrame *callframe)) { + auto throwScope = DECLARE_THROW_SCOPE(lexicalGlobalObject->vm()); + + auto buffer = callframe->argument(0); + auto *bufferView = JSC::jsDynamicCast<JSC::JSArrayBufferView *>(buffer); + const char *ptr = nullptr; + size_t byteLength = 0; + if (bufferView) { + + if (UNLIKELY(bufferView->isDetached())) { + throwTypeError(lexicalGlobalObject, throwScope, + "ArrayBufferView is detached"_s); + return JSValue::encode({}); + } + + byteLength = bufferView->byteLength(); + + if (byteLength == 0) { + return JSValue::encode(jsBoolean(true)); + } + + ptr = reinterpret_cast<const char *>(bufferView->vector()); + } else if (auto *arrayBuffer = + JSC::jsDynamicCast<JSC::JSArrayBuffer *>(buffer)) { + auto *impl = arrayBuffer->impl(); + if (UNLIKELY(impl->isDetached())) { + throwTypeError(lexicalGlobalObject, throwScope, + "ArrayBuffer is detached"_s); + return JSValue::encode({}); + } + + if (!impl) { + return JSValue::encode(jsBoolean(true)); + } + + byteLength = impl->byteLength(); + + if (byteLength == 0) { + return JSValue::encode(jsBoolean(true)); + } + + ptr = reinterpret_cast<const char *>(impl->data()); + } else { + throwVMError( + lexicalGlobalObject, throwScope, + createTypeError(lexicalGlobalObject, + "First argument must be an ArrayBufferView"_s)); + return JSValue::encode({}); + } + + RELEASE_AND_RETURN( + throwScope, + JSValue::encode(jsBoolean(simdutf::validate_ascii(ptr, byteLength)))); +} + JSC_DEFINE_HOST_FUNCTION(jsFunctionNotImplemented, (JSGlobalObject * globalObject, CallFrame *callFrame)) { @@ -29,10 +145,13 @@ inline void generateBufferSourceCode(JSC::JSGlobalObject *lexicalGlobalObject, JSC::JSObject *defaultObject = JSC::constructEmptyObject( globalObject, globalObject->objectPrototype(), 12); - defaultObject->putDirect(vm, - PropertyName(Identifier::fromUid( - vm.symbolRegistry().symbolForKey("CommonJS"_s))), - jsNumber(0), 0); + auto CommonJS = + Identifier::fromUid(vm.symbolRegistry().symbolForKey("CommonJS"_s)); + + defaultObject->putDirect(vm, PropertyName(CommonJS), jsNumber(0), 0); + + exportNames.append(CommonJS); + exportValues.append(jsNumber(0)); auto exportProperty = [&](JSC::Identifier name, JSC::JSValue value) { exportNames.append(name); @@ -103,6 +222,20 @@ inline void generateBufferSourceCode(JSC::JSGlobalObject *lexicalGlobalObject, exportProperty(JSC::Identifier::fromString(vm, "resolveObjectURL"_s), resolveObjectURL); + exportProperty(JSC::Identifier::fromString(vm, "isAscii"_s), + JSC::JSFunction::create(vm, globalObject, 1, "isAscii"_s, + jsBufferConstructorFunction_isAscii, + ImplementationVisibility::Public, + NoIntrinsic, + jsBufferConstructorFunction_isUtf8)); + + exportProperty(JSC::Identifier::fromString(vm, "isUtf8"_s), + JSC::JSFunction::create(vm, globalObject, 1, "isUtf8"_s, + jsBufferConstructorFunction_isUtf8, + ImplementationVisibility::Public, + NoIntrinsic, + jsBufferConstructorFunction_isUtf8)); + exportNames.append(vm.propertyNames->defaultKeyword); exportValues.append(defaultObject); } diff --git a/src/bun.js/modules/NodeModuleModule.cpp b/src/bun.js/modules/NodeModuleModule.cpp index f11277709..3019ea95c 100644 --- a/src/bun.js/modules/NodeModuleModule.cpp +++ b/src/bun.js/modules/NodeModuleModule.cpp @@ -19,34 +19,15 @@ JSC_DEFINE_HOST_FUNCTION(jsFunctionNodeModuleCreateRequire, return JSC::JSValue::encode(JSC::jsUndefined()); } - auto str = callFrame->uncheckedArgument(0).toStringOrNull(globalObject); + auto val = callFrame->uncheckedArgument(0).toWTFString(globalObject); RETURN_IF_EXCEPTION(scope, JSC::JSValue::encode(JSC::jsUndefined())); - WTF::String val = str->value(globalObject); - auto *meta = Zig::ImportMetaObject::create(globalObject, str); auto clientData = WebCore::clientData(vm); - auto requireFunction = - Zig::ImportMetaObject::createRequireFunction(vm, globalObject, val); - auto nameStr = jsCast<JSFunction *>(requireFunction)->name(vm); - JSC::JSBoundFunction *boundRequireFunction = - JSC::JSBoundFunction::create(vm, globalObject, requireFunction, meta, - ArgList(), 0, jsString(vm, nameStr)); - boundRequireFunction->putDirect( - vm, clientData->builtinNames().resolvePublicName(), - requireFunction->getDirect( - vm, clientData->builtinNames().resolvePublicName()), - 0); - - RELEASE_AND_RETURN(scope, JSValue::encode(boundRequireFunction)); -} -JSC_DEFINE_HOST_FUNCTION(jsFunctionNodeModulePaths, - (JSC::JSGlobalObject * globalObject, - JSC::CallFrame *callFrame)) { - return JSC::JSValue::encode(JSC::JSArray::create( - globalObject->vm(), - globalObject->arrayStructureForIndexingTypeDuringAllocation( - ArrayWithContiguous), - 0)); + RELEASE_AND_RETURN( + scope, JSValue::encode(Zig::ImportMetaObject::createRequireFunction( + vm, globalObject, val))); } +extern "C" EncodedJSValue Resolver__nodeModulePathsForJS(JSGlobalObject *, + CallFrame *); JSC_DEFINE_HOST_FUNCTION(jsFunctionFindSourceMap, (JSGlobalObject * globalObject, @@ -122,48 +103,56 @@ void generateNodeModuleModule(JSC::JSGlobalObject *globalObject, JSC::MarkedArgumentBuffer &exportValues) { JSC::VM &vm = globalObject->vm(); - exportValues.append(JSFunction::create( - vm, globalObject, 1, String("createRequire"_s), - jsFunctionNodeModuleCreateRequire, ImplementationVisibility::Public)); - exportValues.append(JSFunction::create(vm, globalObject, 1, String("paths"_s), - jsFunctionNodeModulePaths, - ImplementationVisibility::Public)); - exportValues.append(JSFunction::create( - vm, globalObject, 1, String("findSourceMap"_s), jsFunctionFindSourceMap, - ImplementationVisibility::Public)); - exportValues.append(JSFunction::create( - vm, globalObject, 0, String("syncBuiltinExports"_s), - jsFunctionSyncBuiltinExports, ImplementationVisibility::Public)); - exportValues.append( - JSFunction::create(vm, globalObject, 1, String("SourceMap"_s), - jsFunctionSourceMap, ImplementationVisibility::Public, - NoIntrinsic, jsFunctionSourceMap, nullptr)); - - exportNames.append(JSC::Identifier::fromString(vm, "createRequire"_s)); - exportNames.append(JSC::Identifier::fromString(vm, "paths"_s)); - exportNames.append(JSC::Identifier::fromString(vm, "findSourceMap"_s)); - exportNames.append(JSC::Identifier::fromString(vm, "syncBuiltinExports"_s)); - exportNames.append(JSC::Identifier::fromString(vm, "SourceMap"_s)); - - // note: this is not technically correct - // it doesn't set process.mainModule - exportNames.append(JSC::Identifier::fromString(vm, "_resolveFileName"_s)); - exportValues.append(JSFunction::create( - vm, globalObject, 3, String("_resolveFileName"_s), - jsFunctionResolveFileName, ImplementationVisibility::Public)); - - exportNames.append(JSC::Identifier::fromString(vm, "_nodeModulePaths"_s)); - exportValues.append(JSFunction::create( - vm, globalObject, 0, String("_nodeModulePaths"_s), - jsFunctionNodeModulePaths, ImplementationVisibility::Public)); - - exportNames.append(JSC::Identifier::fromString(vm, "_cache"_s)); - exportValues.append(JSC::constructEmptyObject(globalObject)); - - exportNames.append(JSC::Identifier::fromString(vm, "builtinModules"_s)); - - exportNames.append(JSC::Identifier::fromString(vm, "globalPaths"_s)); - exportValues.append(JSC::constructEmptyArray(globalObject, 0)); + JSObject *defaultObject = JSC::constructEmptyObject( + vm, globalObject->nullPrototypeObjectStructure()); + auto append = [&](Identifier name, JSValue value) { + defaultObject->putDirect(vm, name, value); + exportNames.append(name); + exportValues.append(value); + }; + + append(Identifier::fromString(vm, "createRequire"_s), + JSFunction::create(vm, globalObject, 1, String("createRequire"_s), + jsFunctionNodeModuleCreateRequire, + ImplementationVisibility::Public)); + + append(Identifier::fromString(vm, "paths"_s), + JSFunction::create(vm, globalObject, 1, String("paths"_s), + Resolver__nodeModulePathsForJS, + ImplementationVisibility::Public)); + + append(Identifier::fromString(vm, "findSourceMap"_s), + JSFunction::create(vm, globalObject, 1, String("findSourceMap"_s), + jsFunctionFindSourceMap, + ImplementationVisibility::Public)); + append(Identifier::fromString(vm, "syncBuiltinExports"_s), + JSFunction::create(vm, globalObject, 0, String("syncBuiltinExports"_s), + jsFunctionSyncBuiltinExports, + ImplementationVisibility::Public)); + append(Identifier::fromString(vm, "SourceMap"_s), + JSFunction::create(vm, globalObject, 1, String("SourceMap"_s), + jsFunctionSourceMap, + ImplementationVisibility::Public, NoIntrinsic, + jsFunctionSourceMap, nullptr)); + + append(JSC::Identifier::fromString(vm, "_resolveFilename"_s), + JSFunction::create(vm, globalObject, 3, String("_resolveFilename"_s), + jsFunctionResolveFileName, + ImplementationVisibility::Public)); + + append(JSC::Identifier::fromString(vm, "_nodeModulePaths"_s), + JSFunction::create(vm, globalObject, 0, String("_nodeModulePaths"_s), + Resolver__nodeModulePathsForJS, + ImplementationVisibility::Public)); + + append(JSC::Identifier::fromString(vm, "_cache"_s), + jsCast<Zig::GlobalObject *>(globalObject)->lazyRequireCacheObject()); + + append(JSC::Identifier::fromString(vm, "globalPaths"_s), + JSC::constructEmptyArray(globalObject, nullptr, 0)); + + append(JSC::Identifier::fromString(vm, "prototype"_s), + JSC::constructEmptyObject(globalObject)); JSC::JSArray *builtinModules = JSC::JSArray::create( vm, @@ -184,6 +173,15 @@ void generateNodeModuleModule(JSC::JSGlobalObject *globalObject, JSC::jsString(vm, String("bun:ffi"_s))); builtinModules->putDirectIndex(globalObject, 6, JSC::jsString(vm, String("bun:sqlite"_s))); - exportValues.append(builtinModules); + + append(JSC::Identifier::fromString(vm, "builtinModules"_s), builtinModules); + + defaultObject->putDirect(vm, + JSC::PropertyName(Identifier::fromUid( + vm.symbolRegistry().symbolForKey("CommonJS"_s))), + jsNumber(0), 0); + + exportNames.append(vm.propertyNames->defaultKeyword); + exportValues.append(defaultObject); } } // namespace Zig diff --git a/src/bun.js/modules/ProcessModule.h b/src/bun.js/modules/ProcessModule.h index 3c9c3261f..fab0298ae 100644 --- a/src/bun.js/modules/ProcessModule.h +++ b/src/bun.js/modules/ProcessModule.h @@ -44,32 +44,37 @@ inline void generateProcessSourceCode(JSC::JSGlobalObject *lexicalGlobalObject, reinterpret_cast<GlobalObject *>(lexicalGlobalObject); JSC::JSObject *process = globalObject->processObject(); + auto scope = DECLARE_THROW_SCOPE(vm); + if (!process->staticPropertiesReified()) { + process->reifyAllStaticProperties(globalObject); + if (scope.exception()) + return; + } PropertyNameArray properties(vm, PropertyNameMode::Strings, PrivateSymbolMode::Exclude); process->getPropertyNames(globalObject, properties, DontEnumPropertiesMode::Exclude); + if (scope.exception()) + return; + + exportNames.append(vm.propertyNames->defaultKeyword); + exportValues.append(process); - exportNames.append(JSC::Identifier::fromString(vm, "default"_s)); - JSFunction *processModuleCommonJS = JSFunction::create( - vm, globalObject, 0, "process"_s, jsFunctionProcessModuleCommonJS, - ImplementationVisibility::Public); - processModuleCommonJS->putDirect( - vm, - PropertyName( - Identifier::fromUid(vm.symbolRegistry().symbolForKey("CommonJS"_s))), - jsBoolean(true), 0); - exportValues.append(processModuleCommonJS); + exportNames.append( + Identifier::fromUid(vm.symbolRegistry().symbolForKey("CommonJS"_s))); + exportValues.append(jsNumber(0)); for (auto &entry : properties) { exportNames.append(entry); - exportValues.append(process->get(globalObject, entry)); - processModuleCommonJS->putDirectCustomAccessor( - vm, entry, - JSC::CustomGetterSetter::create(vm, - jsFunctionProcessModuleCommonJSGetter, - jsFunctionProcessModuleCommonJSSetter), - 0); + auto catchScope = DECLARE_CATCH_SCOPE(vm); + JSValue result = process->get(globalObject, entry); + if (catchScope.exception()) { + result = jsUndefined(); + catchScope.clearException(); + } + + exportValues.append(result); } } diff --git a/src/bun.js/modules/StringDecoderModule.h b/src/bun.js/modules/StringDecoderModule.h index c3b5f57bb..1dbf5ef8e 100644 --- a/src/bun.js/modules/StringDecoderModule.h +++ b/src/bun.js/modules/StringDecoderModule.h @@ -16,11 +16,11 @@ generateStringDecoderSourceCode(JSC::JSGlobalObject *lexicalGlobalObject, exportNames.append(JSC::Identifier::fromString(vm, "StringDecoder"_s)); exportValues.append(globalObject->JSStringDecoder()); + auto CommonJS = + Identifier::fromUid(vm.symbolRegistry().symbolForKey("CommonJS"_s)); + JSC::JSObject *defaultObject = constructEmptyObject(globalObject); - defaultObject->putDirect(vm, - PropertyName(Identifier::fromUid( - vm.symbolRegistry().symbolForKey("CommonJS"_s))), - jsNumber(0), 0); + defaultObject->putDirect(vm, PropertyName(CommonJS), jsNumber(0), 0); for (size_t i = 0; i < exportNames.size(); i++) { defaultObject->putDirect(vm, exportNames[i], exportValues.at(i), 0); @@ -28,6 +28,9 @@ generateStringDecoderSourceCode(JSC::JSGlobalObject *lexicalGlobalObject, exportNames.append(vm.propertyNames->defaultKeyword); exportValues.append(defaultObject); + + exportNames.append(CommonJS); + exportValues.append(jsNumber(0)); } } // namespace Zig diff --git a/src/bun.js/modules/TTYModule.h b/src/bun.js/modules/TTYModule.h index 423268b32..79bc8c871 100644 --- a/src/bun.js/modules/TTYModule.h +++ b/src/bun.js/modules/TTYModule.h @@ -62,17 +62,20 @@ inline void generateTTYSourceCode(JSC::JSGlobalObject *lexicalGlobalObject, tty->putDirect(vm, JSC::Identifier::fromString(vm, "WriteStream"_s), notimpl); exportValues.append(notimpl); - tty->putDirect(vm, - PropertyName(Identifier::fromUid( - vm.symbolRegistry().symbolForKey("CommonJS"_s))), - jsNumber(0), 0); - for (size_t i = 0; i < exportNames.size(); i++) { tty->putDirect(vm, exportNames[i], exportValues.at(i), 0); } exportNames.append(vm.propertyNames->defaultKeyword); exportValues.append(tty); + + auto CommonJS = + Identifier::fromUid(vm.symbolRegistry().symbolForKey("CommonJS"_s)); + + exportNames.append(CommonJS); + exportValues.append(jsNumber(0)); + + tty->putDirect(vm, PropertyName(CommonJS), jsNumber(0), 0); } } // namespace Zig |