aboutsummaryrefslogtreecommitdiff
path: root/src/javascript/jsc/bindings/ZigGlobalObject.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/javascript/jsc/bindings/ZigGlobalObject.cpp')
-rw-r--r--src/javascript/jsc/bindings/ZigGlobalObject.cpp40
1 files changed, 38 insertions, 2 deletions
diff --git a/src/javascript/jsc/bindings/ZigGlobalObject.cpp b/src/javascript/jsc/bindings/ZigGlobalObject.cpp
index fbe3c22ae..45a68954e 100644
--- a/src/javascript/jsc/bindings/ZigGlobalObject.cpp
+++ b/src/javascript/jsc/bindings/ZigGlobalObject.cpp
@@ -121,9 +121,40 @@ void GlobalObject::setConsole(void *console) {
this->setConsoleClient(makeWeakPtr(m_console));
}
+// This is not a publicly exposed API currently.
+// This is used by the bundler to make Response, Request, FetchEvent,
+// and any other objects available globally.
void GlobalObject::installAPIGlobals(JSClassRef *globals, int count) {
WTF::Vector<GlobalPropertyInfo> extraStaticGlobals;
- extraStaticGlobals.reserveCapacity((size_t)count);
+ extraStaticGlobals.reserveCapacity((size_t)count + 1);
+
+ // This is not nearly a complete implementation. It's just enough to make some npm packages that
+ // were compiled with Webpack to run without crashing in this environment.
+ JSC::JSObject *process = JSC::constructEmptyObject(this, this->objectPrototype(), 4);
+
+ // The transpiler inlines all defined process.env vars & dead code eliminates as relevant
+ // so this is just to return undefined for any missing ones and not crash if something tries to
+ // modify it or it wasn't statically analyzable
+ JSC::JSObject *processDotEnv = JSC::constructEmptyObject(this, this->objectPrototype(), 0);
+
+ process->putDirect(this->vm(), JSC::Identifier::fromString(this->vm(), "env"), processDotEnv);
+
+ // this should be transpiled out, but just incase
+ process->putDirect(this->vm(), JSC::Identifier::fromString(this->vm(), "browser"),
+ JSC::JSValue(false));
+
+ // this gives some way of identifying at runtime whether the SSR is happening in node or not.
+ // this should probably be renamed to what the name of the bundler is, instead of "notNodeJS"
+ // but it must be something that won't evaluate to truthy in Node.js
+ process->putDirect(this->vm(), JSC::Identifier::fromString(this->vm(), "notNodeJS"),
+ JSC::JSValue(true));
+#if defined(__APPLE__)
+ process->putDirect(this->vm(), JSC::Identifier::fromString(this->vm(), "platform"),
+ JSC::jsString(this->vm(), WTF::String("darwin")));
+#else
+ process->putDirect(this->vm(), JSC::Identifier::fromString(this->vm(), "platform"),
+ JSC::jsString(this->vm(), WTF::String("linux")));
+#endif
for (int i = 0; i < count; i++) {
auto jsClass = globals[i];
@@ -137,7 +168,12 @@ void GlobalObject::installAPIGlobals(JSClassRef *globals, int count) {
GlobalPropertyInfo{JSC::Identifier::fromString(vm(), jsClass->className()),
JSC::JSValue(object), JSC::PropertyAttribute::DontDelete | 0});
}
- this->addStaticGlobals(extraStaticGlobals.data(), count);
+
+ extraStaticGlobals.uncheckedAppend(
+ GlobalPropertyInfo{JSC::Identifier::fromString(vm(), "process"), JSC::JSValue(process),
+ JSC::PropertyAttribute::DontDelete | 0});
+
+ this->addStaticGlobals(extraStaticGlobals.data(), extraStaticGlobals.size());
extraStaticGlobals.releaseBuffer();
}