fs.*Sync(), bun wiptest, and More ™ (#106)

* very very wip

* almost ready to fix the errors

* Update identity_context.zig

* Update base.zig

* [bun test] It runs successfully

* Remove unnecessary call

* [Bun.js] Improve JS <> Zig unicode string interop

This fixes longstanding unicode bugs with `console.log` & `fetch`.

I believe @evanwashere reported this first awhile ago

* [Bun.js] Implement `Object.is()` binding and a way to set a timeout for script execution

* Update PLCrashReport.zig

* [Bun.js] Make `console.log` more closely match Node.js and Deno

* [Bun.js] Implement formatting specifier for console.*

* Implement `console.clear()`

* bug fix

* Support console.clear()

* Buffer stderr

* [bun test] Begin implementing Node.js `fs`

* Update darwin_c.zig

* Implement more of `fs`

* `mkdir`, `mkdir` recursive, `mkdtemp`

* `open`, `read` (and pread)

* Move some things into more files

* Implement readdir

* `readFile`, `readLink`, and `realpath`

* `writeFile`, `symlink`, `chown`, `rename`, `stat`, `unlink`, `truncate`

* `lutimes`

* Implement `SystemError` and begin wiring up the `fs` module

* `"fs"` - Most of the arguments / validation

* `fs` - Rest of the arguments / validations

* Begin wiring up the `fs` module

* Fix all the build errors

* support printing typed arrays in console.log

* It...works?

* Support `require("fs")`, `import fs from 'fs';`, `import * as fs from 'fs'`

* Fix a couple bugs

* get rid of the crash reporter for now

* Update fs.exports.js

* [bun.js] slight improvement to startup time

* [bun.js] Improve error message printing

* [Bun.js] Add `Bun.gc()` to run the garbage collector manually and report heap size

* [Bun.js] Add Bun.generateHeapSnapshot to return what JS types are using memory

* [Bun.js] Add `Bun.shrink()` to tell JSC to shrink the VM size

* Improve encoding reader

* [bun.js] Improve callback & microtask performance

* Update node_fs.zig

* Implement `console.assert`

* simple test

* [Bun.js] Prepare for multiple globals/realms to support testing

* Create callbacks-overhead.mjs

* Update http.zig

* [Bun.js] Implement `queueMicrotask`

* Add test for queueMicrotask

* 😪

* [Bun.js] Implement `process.versions`, `process.pid`, `process.ppid`, `process.nextTick`, `process.versions`,

* Implement `process.env.toJSON()`

* [Bun.js] Improve performance of `fs.existsSync`

* 💅

* [Bun.js] Implement `process.chdir(str)` and `process.cwd()`, support up to 4 args in `process.nextTick`

* Make creating Zig::Process lazy

* Split processi nto separte file

* [Bun.js] Node.js Streams - Part 1/?

* [Bun.js] Node.js streams 2/?

* WIP streams

* fix crash

* Reduce allocations in many places

* swap

* Make `bun` start 2ms faster

* Always use an apiLock()

* libBacktrace doesn't really work yet

* Fix crash in the upgrade checker

* Clean up code for importing the runtime when not bundling

* 📷

* Update linker.zig

* 68!

* backtrace

* no, really backtrace

* Fix

* Linux fixes

* Fixes on Linux

* Update mimalloc

* [bun test] Automatically scan for {.test,_test,.spec,_spec}.{jsx,tsx,js,cts,mts,ts,cjs}
This commit is contained in:
Jarred Sumner
2022-01-19 02:29:07 -08:00
committed by GitHub
parent ed9637de50
commit d3a93d5273
186 changed files with 17241 additions and 2843 deletions

View File

@@ -26,6 +26,7 @@
#include <JavaScriptCore/JSCallbackObject.h>
#include <JavaScriptCore/JSCast.h>
#include <JavaScriptCore/JSClassRef.h>
#include <JavaScriptCore/JSMicrotask.h>
// #include <JavaScriptCore/JSContextInternal.h>
#include <JavaScriptCore/CatchScope.h>
#include <JavaScriptCore/JSInternalPromise.h>
@@ -51,6 +52,7 @@
#include <JavaScriptCore/VM.h>
#include <JavaScriptCore/VMEntryScope.h>
#include <JavaScriptCore/WasmFaultSignalHandler.h>
#include <unistd.h>
#include <wtf/Gigacage.h>
#include <wtf/StdLibExtras.h>
#include <wtf/URL.h>
@@ -68,6 +70,8 @@
#include <JavaScriptCore/JSCallbackObject.h>
#include <JavaScriptCore/JSClassRef.h>
#include "BunClientData.h"
#include "ZigSourceProvider.h"
using JSGlobalObject = JSC::JSGlobalObject;
@@ -82,21 +86,20 @@ using JSObject = JSC::JSObject;
using JSNonFinalObject = JSC::JSNonFinalObject;
namespace JSCastingHelpers = JSC::JSCastingHelpers;
bool has_loaded_jsc = false;
static bool has_loaded_jsc = false;
extern "C" void JSCInitialize() {
if (has_loaded_jsc) return;
has_loaded_jsc = true;
JSC::Options::useSourceProviderCache() = true;
JSC::Options::useUnlinkedCodeBlockJettisoning() = false;
JSC::Options::exposeInternalModuleLoader() = true;
JSC::Options::useSharedArrayBuffer() = true;
// JSC::Options::useAtMethod() = true;
// std::set_terminate([]() { Zig__GlobalObject__onCrash(); });
WTF::initializeMainThread();
JSC::initialize();
// Gigacage::disablePrimitiveGigacage();
has_loaded_jsc = true;
}
extern "C" JSC__JSGlobalObject *Zig__GlobalObject__create(JSClassRef *globalObjectClass, int count,
@@ -104,6 +107,8 @@ extern "C" JSC__JSGlobalObject *Zig__GlobalObject__create(JSClassRef *globalObje
auto heapSize = JSC::LargeHeap;
JSC::VM &vm = JSC::VM::create(heapSize).leakRef();
Bun::JSVMClientData::create(&vm);
vm.heap.acquireAccess();
#if ENABLE(WEBASSEMBLY)
JSC::Wasm::enableFastMemory();
@@ -257,33 +262,80 @@ void GlobalObject::setConsole(void *console) {
this->setConsoleClient(makeWeakPtr(m_console));
}
#pragma mark - Globals
static JSC_DECLARE_CUSTOM_SETTER(property_lazyProcessSetter);
static JSC_DECLARE_CUSTOM_GETTER(property_lazyProcessGetter);
JSC_DEFINE_CUSTOM_SETTER(property_lazyProcessSetter,
(JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue,
JSC::EncodedJSValue value, JSC::PropertyName)) {
return false;
}
static JSClassRef dot_env_class_ref;
JSC_DEFINE_CUSTOM_GETTER(property_lazyProcessGetter,
(JSC::JSGlobalObject * _globalObject, JSC::EncodedJSValue thisValue,
JSC::PropertyName)) {
Zig::GlobalObject *globalObject = reinterpret_cast<Zig::GlobalObject *>(_globalObject);
if (LIKELY(globalObject->m_process))
return JSValue::encode(JSC::JSValue(globalObject->m_process));
JSC::VM &vm = globalObject->vm();
globalObject->m_process = Zig::Process::create(
vm, Zig::Process::createStructure(vm, globalObject, globalObject->objectPrototype()));
{
auto jsClass = dot_env_class_ref;
JSC::JSCallbackObject<JSNonFinalObject> *object =
JSC::JSCallbackObject<JSNonFinalObject>::create(
globalObject, globalObject->callbackObjectStructure(), jsClass, nullptr);
if (JSObject *prototype = jsClass->prototype(globalObject))
object->setPrototypeDirect(vm, prototype);
globalObject->m_process->putDirect(vm, JSC::Identifier::fromString(vm, "env"),
JSC::JSValue(object),
JSC::PropertyAttribute::DontDelete | 0);
}
return JSC::JSValue::encode(JSC::JSValue(globalObject->m_process));
}
static JSC_DECLARE_HOST_FUNCTION(functionQueueMicrotask);
static JSC_DEFINE_HOST_FUNCTION(functionQueueMicrotask,
(JSC::JSGlobalObject * globalObject, JSC::CallFrame *callFrame)) {
JSC::VM &vm = globalObject->vm();
if (callFrame->argumentCount() == 0) {
auto scope = DECLARE_THROW_SCOPE(globalObject->vm());
JSC::throwTypeError(globalObject, scope, "queueMicrotask requires 1 argument (a function)"_s);
return JSC::JSValue::encode(JSC::JSValue{});
}
JSC::JSValue job = callFrame->argument(0);
if (!job.isObject() || !job.getObject()->isCallable(vm)) {
auto scope = DECLARE_THROW_SCOPE(globalObject->vm());
JSC::throwTypeError(globalObject, scope, "queueMicrotask expects a function"_s);
return JSC::JSValue::encode(JSC::JSValue{});
}
// This is a JSC builtin function
globalObject->queueMicrotask(JSC::createJSMicrotask(vm, job));
return JSC::JSValue::encode(JSC::jsUndefined());
}
// 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 + 2);
extraStaticGlobals.reserveCapacity((size_t)count + 3);
// 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);
// 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(), "isBun"),
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
int i = 0;
for (; i < count - 1; i++) {
auto jsClass = globals[i];
@@ -300,23 +352,38 @@ void GlobalObject::installAPIGlobals(JSClassRef *globals, int count) {
// The last one must be "process.env"
// Runtime-support is for if they change
{
auto jsClass = globals[i];
dot_env_class_ref = globals[i];
JSC::JSCallbackObject<JSNonFinalObject> *object =
JSC::JSCallbackObject<JSNonFinalObject>::create(this, this->callbackObjectStructure(),
jsClass, nullptr);
if (JSObject *prototype = jsClass->prototype(this)) object->setPrototypeDirect(vm(), prototype);
// // The last one must be "process.env"
// // Runtime-support is for if they change
// {
// auto jsClass = globals[i];
process->putDirect(this->vm(), JSC::Identifier::fromString(this->vm(), "env"),
JSC::JSValue(object), JSC::PropertyAttribute::DontDelete | 0);
}
// JSC::JSCallbackObject<JSNonFinalObject> *object =
// JSC::JSCallbackObject<JSNonFinalObject>::create(this, this->callbackObjectStructure(),
// jsClass, nullptr);
// if (JSObject *prototype = jsClass->prototype(this)) object->setPrototypeDirect(vm(),
// prototype);
// process->putDirect(this->vm(), JSC::Identifier::fromString(this->vm(), "env"),
// JSC::JSValue(object), JSC::PropertyAttribute::DontDelete | 0);
// }
JSC::Identifier queueMicrotaskIdentifier = JSC::Identifier::fromString(vm(), "queueMicrotask"_s);
extraStaticGlobals.uncheckedAppend(
GlobalPropertyInfo{JSC::Identifier::fromString(vm(), "process"), JSC::JSValue(process),
GlobalPropertyInfo{queueMicrotaskIdentifier,
JSC::JSFunction::create(vm(), JSC::jsCast<JSC::JSGlobalObject *>(this), 0,
"queueMicrotask", functionQueueMicrotask),
JSC::PropertyAttribute::DontDelete | 0});
auto clientData = Bun::clientData(vm());
this->addStaticGlobals(extraStaticGlobals.data(), extraStaticGlobals.size());
putDirectCustomAccessor(
vm(), clientData->builtinNames().processPublicName(),
JSC::CustomGetterSetter::create(vm(), property_lazyProcessGetter, property_lazyProcessSetter),
JSC::PropertyAttribute::CustomValue | 0);
extraStaticGlobals.releaseBuffer();
}
@@ -418,7 +485,6 @@ JSC::JSObject *GlobalObject::moduleLoaderCreateImportMetaProperties(JSGlobalObje
JSModuleRecord *record,
JSValue val) {
ZigString specifier = Zig::toZigString(key, globalObject);
JSC::VM &vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
@@ -426,7 +492,16 @@ JSC::JSObject *GlobalObject::moduleLoaderCreateImportMetaProperties(JSGlobalObje
JSC::constructEmptyObject(vm, globalObject->nullPrototypeObjectStructure());
RETURN_IF_EXCEPTION(scope, nullptr);
metaProperties->putDirect(vm, Identifier::fromString(vm, "filePath"), key);
auto clientData = Bun::clientData(vm);
JSString *keyString = key.toStringOrNull(globalObject);
if (UNLIKELY(!keyString)) { return metaProperties; }
auto view = keyString->value(globalObject);
auto index = view.reverseFind('/', view.length());
if (index != WTF::notFound) {
metaProperties->putDirect(vm, clientData->builtinNames().dirPublicName(),
JSC::jsSubstring(globalObject, keyString, 0, index));
}
metaProperties->putDirect(vm, clientData->builtinNames().pathPublicName(), key);
RETURN_IF_EXCEPTION(scope, nullptr);
// metaProperties->putDirect(vm, Identifier::fromString(vm, "resolve"),