Skip to content

Commit

Permalink
Further refinement on CommonJS types (#3302)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell authored Jan 7, 2025
1 parent 19fc71c commit 8a194ae
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 43 deletions.
56 changes: 38 additions & 18 deletions src/workerd/jsg/commonjs.c++
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

namespace workerd::jsg {

CommonJsModuleContext::CommonJsModuleContext(jsg::Lock& js, kj::Path path)
: module(jsg::alloc<CommonJsModuleObject>(js, path.toString(true))),
path(kj::mv(path)),
exports(js.v8Isolate, module->getExports(js)) {}

v8::Local<v8::Value> CommonJsModuleContext::require(jsg::Lock& js, kj::String specifier) {
auto modulesForResolveCallback = getModulesForResolveCallback(js.v8Isolate);
KJ_REQUIRE(modulesForResolveCallback != nullptr, "didn't expect resolveCallback() now");
Expand Down Expand Up @@ -42,18 +47,48 @@ v8::Local<v8::Value> 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<CommonJsModuleObject> CommonJsModuleContext::getModule(jsg::Lock& js) {
return module.addRef();
}

v8::Local<v8::Value> 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<v8::Value> CommonJsModuleObject::getExports(jsg::Lock& js) {
v8::Local<v8::Value> 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);
}

// ======================================================================================
Expand Down Expand Up @@ -109,21 +144,6 @@ v8::Local<v8::Value> NodeJsModuleContext::require(jsg::Lock& js, kj::String spec
return ModuleRegistry::requireImpl(js, info, ModuleRegistry::RequireImplOptions::EXPORT_DEFAULT);
}

v8::Local<v8::Value> 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<v8::Object>();
auto buffer = js.v8Get(module, "Buffer"_kj);
JSG_REQUIRE(buffer->IsFunction(), TypeError, "Invalid node:buffer implementation");
return buffer;
}

v8::Local<v8::Value> 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);
}
Expand Down
42 changes: 17 additions & 25 deletions src/workerd/jsg/commonjs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<v8::Value> getExports(jsg::Lock& js);
v8::Local<v8::Value> 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<CommonJsModuleObject>(js)),
path(kj::mv(path)),
exports(js.v8Isolate, module->getExports(js)) {}
CommonJsModuleContext(jsg::Lock& js, kj::Path path);

v8::Local<v8::Value> require(jsg::Lock& js, kj::String specifier);

jsg::Ref<CommonJsModuleObject> getModule(jsg::Lock& js) {
return module.addRef();
}
jsg::Ref<CommonJsModuleObject> getModule(jsg::Lock& js);

v8::Local<v8::Value> getExports(jsg::Lock& js) {
return exports.getHandle(js);
}
void setExports(jsg::Value value) {
exports = kj::mv(value);
}
v8::Local<v8::Value> 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<CommonJsModuleObject> module;

void visitForMemoryInfo(MemoryTracker& tracker) const {
tracker.trackField("exports", exports);
tracker.trackFieldWithSize("path", path.size());
}
void visitForMemoryInfo(MemoryTracker& tracker) const;

private:
kj::Path path;
Expand Down Expand Up @@ -121,8 +117,6 @@ class NodeJsModuleContext: public jsg::Object {
NodeJsModuleContext(jsg::Lock& js, kj::Path path);

v8::Local<v8::Value> require(jsg::Lock& js, kj::String specifier);
v8::Local<v8::Value> getBuffer(jsg::Lock& js);
v8::Local<v8::Value> getProcess(jsg::Lock& js);

// TODO(soon): Implement setImmediate/clearImmediate

Expand All @@ -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);
}
Expand Down

0 comments on commit 8a194ae

Please sign in to comment.