Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/source-lints.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ on:
- "src/**/*.cpp"
- "src/**/*.classes.ts"
- "src/codegen/**"
- "src/js/builtins.d.ts"
- "src/js/**"
- "src/jsc/bindings/**"
- "packages/bun-types/redis.d.ts"
- "scripts/build/**"
Expand All @@ -41,7 +41,7 @@ on:
- "src/**/*.cpp"
- "src/**/*.classes.ts"
- "src/codegen/**"
- "src/js/builtins.d.ts"
- "src/js/**"
- "src/jsc/bindings/**"
- "packages/bun-types/redis.d.ts"
- "scripts/build/**"
Expand Down
3 changes: 2 additions & 1 deletion src/codegen/bundle-modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,8 @@ writeIfNotChanged(
//
// A debug build's runtime reads the module sources from disk instead (BUN_DYNAMIC_JS_LOAD_PATH), but they are still
// here so that a debug bun works as a `--compile` target like any other.
const BUILTINS_FORMAT_VERSION = 1;
// Bump for a layout change, and when the sources gain a private @name: an older bun cannot parse it to make bytecode.
const BUILTINS_FORMAT_VERSION = 2;
const BUILTINS_HEADER_SIZE = 48;

// Identifies these module sources to bytecode generated from them ahead of time (bun build --compile embeds bytecode for
Expand Down
3 changes: 3 additions & 0 deletions src/codegen/replacements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ export const globalsToPrefix = [
"WritableStream",
"WritableStreamDefaultController",
"WritableStreamDefaultWriter",
"clearImmediate",
"isFinite",
"queueMicrotask",
"setImmediate",
"undefined",
];

Expand Down
2 changes: 1 addition & 1 deletion src/exe_format/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::pe::{DOSHeader, PEHeader, SectionHeader};
use crate::read_struct;

const MAGIC: &[u8; 8] = b"BUNBLTNS";
const FORMAT_VERSION: u32 = 1;
const FORMAT_VERSION: u32 = 2;
Comment thread
robobun marked this conversation as resolved.
Resolved
const HEADER_SIZE: usize = 48;
const RECORD_SIZE: usize = 6 * 4;

Expand Down
1 change: 1 addition & 0 deletions src/js/builtins.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ declare var $Buffer: {
new (size: number): Buffer;
new (string: string, encoding?: BufferEncoding): Buffer;
};
declare var $setImmediate: typeof setImmediate;

declare interface Error {
code?: string;
Expand Down
3 changes: 3 additions & 0 deletions src/js/builtins/BunBuiltinNames.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ using namespace JSC;
macro(bytes) \
macro(cancel) \
macro(checks) \
macro(clearImmediate) \
macro(close) \
macro(cmd) \
macro(code) \
Expand Down Expand Up @@ -154,6 +155,7 @@ using namespace JSC;
macro(processBindingConstants) \
macro(props) \
macro(pull) \
macro(queueMicrotask) \
macro(rawHeaders) \
macro(read) \
macro(readable) \
Expand All @@ -169,6 +171,7 @@ using namespace JSC;
macro(sameSite) \
macro(secure) \
macro(self) \
macro(setImmediate) \
macro(sharedFd) \
macro(signal) \
macro(size) \
Expand Down
2 changes: 1 addition & 1 deletion src/js/node/timers.promises.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { validateBoolean, validateAbortSignal, validateObject, validateNumber } =
const { resistStopPropagation } = require("internal/shared");

const symbolAsyncIterator = Symbol.asyncIterator;
const setImmediateGlobal = globalThis.setImmediate;
const setImmediateGlobal = $setImmediate;
const setTimeoutGlobal = globalThis.setTimeout;
const setIntervalGlobal = globalThis.setInterval;

Expand Down
48 changes: 48 additions & 0 deletions src/jsc/bindings/ZigGlobalObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1409,6 +1409,11 @@ JSC_DEFINE_HOST_FUNCTION(functionQueueMicrotask,
return JSC::JSValue::encode(JSC::jsUndefined());
}

static JSValue createQueueMicrotaskFunction(VM& vm, JSObject* globalObject)
{
return JSFunction::create(vm, globalObject->globalObject(), 1, "queueMicrotask"_s, functionQueueMicrotask, ImplementationVisibility::Public);
}

using MicrotaskCallback = void (*)(void*);

JSC_DEFINE_HOST_FUNCTION(functionNativeMicrotaskTrampoline,
Expand Down Expand Up @@ -2856,6 +2861,46 @@ BUN_DEFINE_LAZY_GLOBAL_BUILTIN_GETTER(getLoadEsmIntoCjsBuiltin, commonJSLoadEsmI
BUN_DEFINE_LAZY_GLOBAL_BUILTIN_GETTER(getInternalRequireBuiltin, commonJSInternalRequireCodeGenerator, PropertyAttribute::Builtin | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly)
#undef BUN_DEFINE_LAZY_GLOBAL_BUILTIN_GETTER

// Built-in JS reads these as `@name` (globalsToPrefix), so replacing the public global cannot redirect the runtime's own scheduling.
#define BUN_DEFINE_LAZY_PRIVATE_GLOBAL_FUNCTION(getterName, createFunction) \
JSC_DEFINE_CUSTOM_GETTER(getterName, (JSGlobalObject * globalObject, EncodedJSValue, PropertyName name)) \
{ \
auto& vm = JSC::getVM(globalObject); \
JSValue function = createFunction(vm, globalObject); \
globalObject->putDirect(vm, name, function, PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly); \
return JSValue::encode(function); \
}
BUN_DEFINE_LAZY_PRIVATE_GLOBAL_FUNCTION(getClearImmediatePrivate, Bun::createClearImmediateFunction)
BUN_DEFINE_LAZY_PRIVATE_GLOBAL_FUNCTION(getQueueMicrotaskPrivate, createQueueMicrotaskFunction)
BUN_DEFINE_LAZY_PRIVATE_GLOBAL_FUNCTION(getSetImmediatePrivate, Bun::createSetImmediateFunction)
#undef BUN_DEFINE_LAZY_PRIVATE_GLOBAL_FUNCTION

// The public global is the same object as the private one; `get` (not `getDirect`) so the lazy getter runs.
static JSValue privateGlobalFunction(VM& vm, JSObject* globalObject, const Identifier& privateName)
{
auto scope = DECLARE_TOP_EXCEPTION_SCOPE(vm);
JSValue function = globalObject->get(globalObject->globalObject(), privateName);
// An own property whose getter only allocates.
scope.assertNoExceptionExceptTermination();
RETURN_IF_EXCEPTION(scope, {});
return function;
}
Comment thread
robobun marked this conversation as resolved.
Resolved

static JSValue clearImmediateGlobalFunction(VM& vm, JSObject* globalObject)
{
return privateGlobalFunction(vm, globalObject, WebCore::builtinNames(vm).clearImmediatePrivateName());
}

static JSValue queueMicrotaskGlobalFunction(VM& vm, JSObject* globalObject)
{
return privateGlobalFunction(vm, globalObject, WebCore::builtinNames(vm).queueMicrotaskPrivateName());
}

static JSValue setImmediateGlobalFunction(VM& vm, JSObject* globalObject)
{
return privateGlobalFunction(vm, globalObject, WebCore::builtinNames(vm).setImmediatePrivateName());
}

JSC_DEFINE_HOST_FUNCTION(jsFunctionToClass, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
{
// Mimick the behavior of class Foo {} for a regular JSFunction.
Expand Down Expand Up @@ -3032,6 +3077,9 @@ void GlobalObject::addBuiltinGlobals(JSC::VM& vm)
putDirectCustomAccessor(vm, builtinNames.WritableStreamDefaultControllerPrivateName(), CustomGetterSetter::create(vm, WritableStreamDefaultController_getter, nullptr), attributesForStructure(PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly) | PropertyAttribute::CustomValue);
putDirectCustomAccessor(vm, builtinNames.WritableStreamDefaultWriterPrivateName(), CustomGetterSetter::create(vm, WritableStreamDefaultWriter_getter, nullptr), attributesForStructure(PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly) | PropertyAttribute::CustomValue);
putDirectCustomAccessor(vm, builtinNames.AbortSignalPrivateName(), CustomGetterSetter::create(vm, AbortSignal_getter, nullptr), PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly | PropertyAttribute::CustomValue);
putDirectCustomAccessor(vm, builtinNames.clearImmediatePrivateName(), CustomGetterSetter::create(vm, getClearImmediatePrivate, nullptr), PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly | PropertyAttribute::CustomValue);
putDirectCustomAccessor(vm, builtinNames.queueMicrotaskPrivateName(), CustomGetterSetter::create(vm, getQueueMicrotaskPrivate, nullptr), PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly | PropertyAttribute::CustomValue);
putDirectCustomAccessor(vm, builtinNames.setImmediatePrivateName(), CustomGetterSetter::create(vm, getSetImmediatePrivate, nullptr), PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly | PropertyAttribute::CustomValue);

// ----- Public Properties -----

Expand Down
6 changes: 3 additions & 3 deletions src/jsc/bindings/ZigGlobalObject.lut.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
alert WebCore__alert Function 1
atob functionATOB Function 1
btoa functionBTOA Function 1
clearImmediate functionClearImmediate Function 1
clearImmediate clearImmediateGlobalFunction PropertyCallback
clearInterval functionClearInterval Function 1
clearTimeout functionClearTimeout Function 1
confirm WebCore__confirm Function 1
dispatchEvent jsFunctionDispatchEvent Function 1
fetch constructBunFetchObject PropertyCallback
postMessage jsFunctionPostMessage Function 1
prompt WebCore__prompt Function 1
queueMicrotask functionQueueMicrotask Function 1
queueMicrotask queueMicrotaskGlobalFunction PropertyCallback
removeEventListener jsFunctionRemoveEventListener Function 2
reportError functionReportError Function 1
setImmediate createSetImmediateFunction PropertyCallback
setImmediate setImmediateGlobalFunction PropertyCallback
setInterval createSetIntervalFunction PropertyCallback
setTimeout createSetTimeoutFunction PropertyCallback
structuredClone WebCore::jsFunctionStructuredClone Function 2
Expand Down
5 changes: 5 additions & 0 deletions src/jsc/bindings/node/NodeTimers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,9 @@ JSValue createSetImmediateFunction(VM& vm, JSObject* globalObject)
return createTimerFunction(vm, globalObject, "setImmediate"_s, functionSetImmediate, setImmediatePromisifyCustomGetter);
}

JSValue createClearImmediateFunction(VM& vm, JSObject* globalObject)
{
return JSFunction::create(vm, globalObject->globalObject(), 1, "clearImmediate"_s, functionClearImmediate, ImplementationVisibility::Public);
}

} // namespace Bun
2 changes: 2 additions & 0 deletions src/jsc/bindings/node/NodeTimers.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ JSC::JSValue createSetTimeoutFunction(JSC::VM&, JSC::JSObject* globalObject);
JSC::JSValue createSetIntervalFunction(JSC::VM&, JSC::JSObject* globalObject);
JSC::JSValue createSetImmediateFunction(JSC::VM&, JSC::JSObject* globalObject);

JSC::JSValue createClearImmediateFunction(JSC::VM&, JSC::JSObject* globalObject);

} // namespace Bun
186 changes: 186 additions & 0 deletions test/internal/source-lints/builtins-format-version.inventory.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
{
"formatVersion": 2,
"privateNames": [
"$$typeof",
"AbortSignal",
"Buffer",
"ReadableByteStreamController",
"ReadableStream",
"ReadableStreamBYOBReader",
"ReadableStreamBYOBRequest",
"ReadableStreamDefaultController",
"ReadableStreamDefaultReader",
"SQL",
"TransformStream",
"TransformStreamDefaultController",
"WritableStream",
"WritableStreamDefaultController",
"WritableStreamDefaultWriter",
"_debugInfo",
"_debugStack",
"_debugTask",
"_events",
"_owner",
"_store",
"abort",
"addAbortAlgorithmToSignal",
"arrayBuffer",
"asUint8Array",
"atimeMs",
"attributes",
"autoAllocateChunkSize",
"basename",
"birthtimeMs",
"blob",
"body",
"bunNativePtr",
"bytes",
"cancel",
"checks",
"clearImmediate",
"close",
"cmd",
"code",
"createCommonJSModule",
"createFIFO",
"createInternalModuleById",
"createUninitializedArrayBuffer",
"ctimeMs",
"data",
"decode",
"dest",
"dirname",
"domain",
"drain",
"encoding",
"end",
"errno",
"esmLoadSync",
"esmNamespaceForCjs",
"esmRegistryDelete",
"esmRegistryEvaluatedKeys",
"esmRegistryHasEvaluated",
"evaluateCommonJSModule",
"evictIsolationSourceProviderCache",
"expires",
"exports",
"extname",
"fastPath",
"fatal",
"fd",
"filename",
"flush",
"format",
"handleEvent",
"headers",
"highWaterMark",
"host",
"hostDefinedImportType",
"hostname",
"httpOnly",
"ignoreBOM",
"importer",
"inherits",
"internal",
"internalMessage",
"internalModuleRegistry",
"internalRequire",
"isAbortSignal",
"isAbsolute",
"isUncloneable",
"isUntransferable",
"join",
"json",
"kResistStopPropagation",
"key",
"lazy",
"lineText",
"loadEsmIntoCjs",
"main",
"makeAbortError",
"makeDOMException",
"makeErrorWithCode",
"makeGetterTypeError",
"masked",
"maxAge",
"maxHeaderPairs",
"metafileJson",
"method",
"min",
"mockedFunction",
"mode",
"moduleGraph",
"mtimeMs",
"napiDlopenHandle",
"napiWrappedContents",
"normalize",
"onClose",
"onDrain",
"originalColumn",
"originalLine",
"overridableRequire",
"parse",
"partitioned",
"path",
"paths",
"peekPromiseSettledValue",
"peekPromiseStatus",
"pokePromiseAsHandled",
"port",
"prev",
"preventAbort",
"preventCancel",
"preventClose",
"processBindingConstants",
"props",
"pull",
"queueMicrotask",
"rawHeaders",
"read",
"readable",
"readableType",
"redirect",
"relative",
"removeAbortAlgorithmFromSignal",
"require",
"requireESM",
"requireMap",
"requireNativeModule",
"resolveSync",
"sameSite",
"secure",
"self",
"setImmediate",
"sharedFd",
"signal",
"size",
"specifier",
"start",
"status",
"statusCode",
"statusMessage",
"statusText",
"storage",
"stream",
"syscall",
"text",
"textDecoderStreamDecoder",
"textEncoderStreamEncoder",
"toClass",
"toNamespacedPath",
"transform",
"type",
"updateRef",
"url",
"validated",
"vmErrorDecorated",
"warning",
"webStreamClosedPromise",
"webStreamControllerError",
"writable",
"writableType",
"write",
"writer",
"written"
]
}
Loading
Loading