From f59892f647ceef1c05e40c9cdef4f79d0a530c2f Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Fri, 13 Aug 2021 02:01:41 -0700 Subject: late Former-commit-id: 1d598bb05a3bac62d86063125e1fe2962f0b5cc6 --- src/javascript/jsc/bindings/ZigGlobalObject.cpp | 40 +++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) (limited to 'src/javascript/jsc/bindings/ZigGlobalObject.cpp') 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 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(); } -- cgit v1.2.3 k/test/add-ed25519-tests Unnamed repository; edit this file 'description' to name the repository.
aboutsummaryrefslogtreecommitdiff
AgeCommit message (Expand)AuthorFilesLines
2022-12-04content-range is inclusiveGravatar Jarred Sumner 1-1/+1
2022-12-04Update README.mdGravatar Jarred Sumner 1-6/+33
2022-12-04[Bun.serve] Implement `Content-Range` support with `Bun.file()`Gravatar Jarred Sumner 5-16/+286
2022-12-04[may revert later] Coerce Infinity to max int 64, -Infinity & NaN to min int64Gravatar Jarred Sumner 1-2/+22
2022-12-03Update .gitignoreGravatar Jarred Sumner 1-0/+1
2022-12-03[test] Add a couple tests for subarray toEqualGravatar Jarred Sumner 1-0/+3
2022-12-03[fetch] Fix bug where .arrayBuffer() on an empty Response body returned a `Ui...Gravatar Jarred Sumner 1-1/+1
2022-12-03Don't invalidate previous file descriptro to avoid tripping assertionGravatar Jarred Sumner 1-5/+0
2022-12-03miscGravatar Jarred Sumner 3-1/+31
2022-12-03Add missing typeGravatar Jarred Sumner 1-0/+5
2022-12-03`process.stdout` and `process.stderr`Gravatar Jarred Sumner 15-564/+1537
2022-12-03simdutf ascii validation is about 20% faster on arm64 than our zig simd @Vect...Gravatar Jarred Sumner 1-0/+3
2022-12-03typo in readme (#1576)Gravatar Reed Jones 1-2/+2