From 8a194aef6daed726a629efcd9afd6a665f327d5d Mon Sep 17 00:00:00 2001 From: James M Snell Date: Tue, 7 Jan 2025 13:58:49 -0800 Subject: [PATCH] Further refinement on CommonJS types (#3302) --- src/workerd/jsg/commonjs.c++ | 56 ++++++++++++++++++++++++------------ src/workerd/jsg/commonjs.h | 42 +++++++++++---------------- 2 files changed, 55 insertions(+), 43 deletions(-) diff --git a/src/workerd/jsg/commonjs.c++ b/src/workerd/jsg/commonjs.c++ index cef1e3951c7..52b4d3e4918 100644 --- a/src/workerd/jsg/commonjs.c++ +++ b/src/workerd/jsg/commonjs.c++ @@ -4,6 +4,11 @@ namespace workerd::jsg { +CommonJsModuleContext::CommonJsModuleContext(jsg::Lock& js, kj::Path path) + : module(jsg::alloc(js, path.toString(true))), + path(kj::mv(path)), + exports(js.v8Isolate, module->getExports(js)) {} + v8::Local CommonJsModuleContext::require(jsg::Lock& js, kj::String specifier) { auto modulesForResolveCallback = getModulesForResolveCallback(js.v8Isolate); KJ_REQUIRE(modulesForResolveCallback != nullptr, "didn't expect resolveCallback() now"); @@ -42,18 +47,48 @@ v8::Local CommonJsModuleContext::require(jsg::Lock& js, kj::String sp return ModuleRegistry::requireImpl(js, info, options); } -CommonJsModuleObject::CommonJsModuleObject(jsg::Lock& js) - : exports(js.v8Isolate, v8::Object::New(js.v8Isolate)) {} +void CommonJsModuleContext::visitForMemoryInfo(MemoryTracker& tracker) const { + tracker.trackField("exports", exports); + tracker.trackFieldWithSize("path", path.size()); +} + +kj::String CommonJsModuleContext::getFilename() const { + return path.toString(true); +} + +kj::String CommonJsModuleContext::getDirname() const { + return path.parent().toString(true); +} + +jsg::Ref CommonJsModuleContext::getModule(jsg::Lock& js) { + return module.addRef(); +} + +v8::Local CommonJsModuleContext::getExports(jsg::Lock& js) const { + return exports.getHandle(js); +} +void CommonJsModuleContext::setExports(jsg::Value value) { + exports = kj::mv(value); +} + +CommonJsModuleObject::CommonJsModuleObject(jsg::Lock& js, kj::String path) + : exports(js.v8Isolate, v8::Object::New(js.v8Isolate)), + path(kj::mv(path)) {} -v8::Local CommonJsModuleObject::getExports(jsg::Lock& js) { +v8::Local CommonJsModuleObject::getExports(jsg::Lock& js) const { return exports.getHandle(js); } void CommonJsModuleObject::setExports(jsg::Value value) { exports = kj::mv(value); } +kj::StringPtr CommonJsModuleObject::getPath() const { + return path; +} + void CommonJsModuleObject::visitForMemoryInfo(MemoryTracker& tracker) const { tracker.trackField("exports", exports); + tracker.trackField("path", path); } // ====================================================================================== @@ -109,21 +144,6 @@ v8::Local NodeJsModuleContext::require(jsg::Lock& js, kj::String spec return ModuleRegistry::requireImpl(js, info, ModuleRegistry::RequireImplOptions::EXPORT_DEFAULT); } -v8::Local NodeJsModuleContext::getBuffer(jsg::Lock& js) { - auto value = require(js, kj::str("node:buffer")); - JSG_REQUIRE(value->IsObject(), TypeError, "Invalid node:buffer implementation"); - auto module = value.As(); - auto buffer = js.v8Get(module, "Buffer"_kj); - JSG_REQUIRE(buffer->IsFunction(), TypeError, "Invalid node:buffer implementation"); - return buffer; -} - -v8::Local NodeJsModuleContext::getProcess(jsg::Lock& js) { - auto value = require(js, kj::str("node:process")); - JSG_REQUIRE(value->IsObject(), TypeError, "Invalid node:process implementation"); - return value; -} - kj::String NodeJsModuleContext::getFilename() { return path.toString(true); } diff --git a/src/workerd/jsg/commonjs.h b/src/workerd/jsg/commonjs.h index e1dc8a4438a..c39209fbad1 100644 --- a/src/workerd/jsg/commonjs.h +++ b/src/workerd/jsg/commonjs.h @@ -6,55 +6,51 @@ namespace workerd::jsg { -class CommonJsModuleObject: public jsg::Object { +class CommonJsModuleObject final: public jsg::Object { public: - CommonJsModuleObject(jsg::Lock& js); + CommonJsModuleObject(jsg::Lock& js, kj::String path); - v8::Local getExports(jsg::Lock& js); + v8::Local getExports(jsg::Lock& js) const; void setExports(jsg::Value value); + kj::StringPtr getPath() const; JSG_RESOURCE_TYPE(CommonJsModuleObject) { JSG_INSTANCE_PROPERTY(exports, getExports, setExports); + JSG_LAZY_READONLY_INSTANCE_PROPERTY(path, getPath); } void visitForMemoryInfo(MemoryTracker& tracker) const; private: jsg::Value exports; + kj::String path; }; -class CommonJsModuleContext: public jsg::Object { +class CommonJsModuleContext final: public jsg::Object { public: - CommonJsModuleContext(jsg::Lock& js, kj::Path path) - : module(jsg::alloc(js)), - path(kj::mv(path)), - exports(js.v8Isolate, module->getExports(js)) {} + CommonJsModuleContext(jsg::Lock& js, kj::Path path); v8::Local require(jsg::Lock& js, kj::String specifier); - jsg::Ref getModule(jsg::Lock& js) { - return module.addRef(); - } + jsg::Ref getModule(jsg::Lock& js); - v8::Local getExports(jsg::Lock& js) { - return exports.getHandle(js); - } - void setExports(jsg::Value value) { - exports = kj::mv(value); - } + v8::Local getExports(jsg::Lock& js) const; + void setExports(jsg::Value value); + + kj::String getFilename() const; + kj::String getDirname() const; JSG_RESOURCE_TYPE(CommonJsModuleContext) { JSG_METHOD(require); JSG_READONLY_INSTANCE_PROPERTY(module, getModule); JSG_INSTANCE_PROPERTY(exports, getExports, setExports); + JSG_LAZY_INSTANCE_PROPERTY(__filename, getFilename); + JSG_LAZY_INSTANCE_PROPERTY(__dirname, getDirname); } jsg::Ref module; - void visitForMemoryInfo(MemoryTracker& tracker) const { - tracker.trackField("exports", exports); - tracker.trackFieldWithSize("path", path.size()); - } + void visitForMemoryInfo(MemoryTracker& tracker) const; private: kj::Path path; @@ -121,8 +117,6 @@ class NodeJsModuleContext: public jsg::Object { NodeJsModuleContext(jsg::Lock& js, kj::Path path); v8::Local require(jsg::Lock& js, kj::String specifier); - v8::Local getBuffer(jsg::Lock& js); - v8::Local getProcess(jsg::Lock& js); // TODO(soon): Implement setImmediate/clearImmediate @@ -138,8 +132,6 @@ class NodeJsModuleContext: public jsg::Object { JSG_METHOD(require); JSG_READONLY_INSTANCE_PROPERTY(module, getModule); JSG_INSTANCE_PROPERTY(exports, getExports, setExports); - JSG_LAZY_INSTANCE_PROPERTY(Buffer, getBuffer); - JSG_LAZY_INSTANCE_PROPERTY(process, getProcess); JSG_LAZY_INSTANCE_PROPERTY(__filename, getFilename); JSG_LAZY_INSTANCE_PROPERTY(__dirname, getDirname); }