mirror of
https://github.com/oven-sh/bun
synced 2026-02-12 03:48:56 +00:00
[Bun.js] Add a stub for setTimeout, setInterval, clearTImeout, clearInterval
This commit is contained in:
@@ -329,12 +329,100 @@ static JSC_DEFINE_HOST_FUNCTION(functionQueueMicrotask,
|
||||
return JSC::JSValue::encode(JSC::jsUndefined());
|
||||
}
|
||||
|
||||
static JSC_DECLARE_HOST_FUNCTION(functionSetTimeout);
|
||||
|
||||
static JSC_DEFINE_HOST_FUNCTION(functionSetTimeout,
|
||||
(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, "setTimeout 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, "setTimeout expects a function"_s);
|
||||
return JSC::JSValue::encode(JSC::JSValue{});
|
||||
}
|
||||
|
||||
if (callFrame->argumentCount() == 1) {
|
||||
globalObject->queueMicrotask(JSC::createJSMicrotask(vm, job));
|
||||
return JSC::JSValue::encode(JSC::jsNumber(Bun__Timer__getNextID()));
|
||||
}
|
||||
|
||||
JSC::JSValue num = callFrame->argument(1);
|
||||
return Bun__Timer__setTimeout(globalObject, JSC::JSValue::encode(job), JSC::JSValue::encode(num));
|
||||
}
|
||||
|
||||
static JSC_DECLARE_HOST_FUNCTION(functionSetInterval);
|
||||
|
||||
static JSC_DEFINE_HOST_FUNCTION(functionSetInterval,
|
||||
(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, "setInterval requires 2 arguments (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, "setInterval expects a function"_s);
|
||||
return JSC::JSValue::encode(JSC::JSValue{});
|
||||
}
|
||||
|
||||
JSC::JSValue num = callFrame->argument(1);
|
||||
return Bun__Timer__setInterval(globalObject, JSC::JSValue::encode(job),
|
||||
JSC::JSValue::encode(num));
|
||||
}
|
||||
|
||||
static JSC_DECLARE_HOST_FUNCTION(functionClearInterval);
|
||||
|
||||
static JSC_DEFINE_HOST_FUNCTION(functionClearInterval,
|
||||
(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, "clearInterval requires 1 argument (a number)"_s);
|
||||
return JSC::JSValue::encode(JSC::JSValue{});
|
||||
}
|
||||
|
||||
JSC::JSValue num = callFrame->argument(0);
|
||||
|
||||
return Bun__Timer__clearInterval(globalObject, JSC::JSValue::encode(num));
|
||||
}
|
||||
|
||||
static JSC_DECLARE_HOST_FUNCTION(functionClearTimeout);
|
||||
|
||||
static JSC_DEFINE_HOST_FUNCTION(functionClearTimeout,
|
||||
(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, "clearTimeout requires 1 argument (a number)"_s);
|
||||
return JSC::JSValue::encode(JSC::JSValue{});
|
||||
}
|
||||
|
||||
JSC::JSValue num = callFrame->argument(0);
|
||||
|
||||
return Bun__Timer__clearTimeout(globalObject, JSC::JSValue::encode(num));
|
||||
}
|
||||
|
||||
// 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 + 3);
|
||||
extraStaticGlobals.reserveCapacity((size_t)count + 3 + 4);
|
||||
|
||||
int i = 0;
|
||||
for (; i < count - 1; i++) {
|
||||
@@ -376,6 +464,34 @@ void GlobalObject::installAPIGlobals(JSClassRef *globals, int count) {
|
||||
"queueMicrotask", functionQueueMicrotask),
|
||||
JSC::PropertyAttribute::DontDelete | 0});
|
||||
|
||||
JSC::Identifier setTimeoutIdentifier = JSC::Identifier::fromString(vm(), "setTimeout"_s);
|
||||
extraStaticGlobals.uncheckedAppend(
|
||||
GlobalPropertyInfo{setTimeoutIdentifier,
|
||||
JSC::JSFunction::create(vm(), JSC::jsCast<JSC::JSGlobalObject *>(this), 0,
|
||||
"setTimeout", functionQueueMicrotask),
|
||||
JSC::PropertyAttribute::DontDelete | 0});
|
||||
|
||||
JSC::Identifier clearTimeoutIdentifier = JSC::Identifier::fromString(vm(), "clearTimeout"_s);
|
||||
extraStaticGlobals.uncheckedAppend(
|
||||
GlobalPropertyInfo{clearTimeoutIdentifier,
|
||||
JSC::JSFunction::create(vm(), JSC::jsCast<JSC::JSGlobalObject *>(this), 0,
|
||||
"clearTimeout", functionQueueMicrotask),
|
||||
JSC::PropertyAttribute::DontDelete | 0});
|
||||
|
||||
JSC::Identifier setIntervalIdentifier = JSC::Identifier::fromString(vm(), "setInterval"_s);
|
||||
extraStaticGlobals.uncheckedAppend(
|
||||
GlobalPropertyInfo{setIntervalIdentifier,
|
||||
JSC::JSFunction::create(vm(), JSC::jsCast<JSC::JSGlobalObject *>(this), 0,
|
||||
"setInterval", functionQueueMicrotask),
|
||||
JSC::PropertyAttribute::DontDelete | 0});
|
||||
|
||||
JSC::Identifier clearIntervalIdentifier = JSC::Identifier::fromString(vm(), "clearInterval"_s);
|
||||
extraStaticGlobals.uncheckedAppend(
|
||||
GlobalPropertyInfo{clearIntervalIdentifier,
|
||||
JSC::JSFunction::create(vm(), JSC::jsCast<JSC::JSGlobalObject *>(this), 0,
|
||||
"clearInterval", functionQueueMicrotask),
|
||||
JSC::PropertyAttribute::DontDelete | 0});
|
||||
|
||||
auto clientData = Bun::clientData(vm());
|
||||
|
||||
this->addStaticGlobals(extraStaticGlobals.data(), extraStaticGlobals.size());
|
||||
|
||||
@@ -1843,11 +1843,15 @@ comptime {
|
||||
@export(ErrorCode.JSErrorObject, .{ .name = "Zig_ErrorCodeJSErrorObject" });
|
||||
}
|
||||
|
||||
const Bun = @import("../javascript.zig").Bun;
|
||||
pub const BunTimer = Bun.Timer;
|
||||
|
||||
comptime {
|
||||
if (!is_bindgen) {
|
||||
_ = Process.getTitle;
|
||||
_ = Process.setTitle;
|
||||
std.testing.refAllDecls(NodeReadableStream);
|
||||
std.testing.refAllDecls(Bun.Timer);
|
||||
std.testing.refAllDecls(NodeWritableStream);
|
||||
std.testing.refAllDecls(NodePath);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//-- AUTOGENERATED FILE -- 1643935973
|
||||
//-- AUTOGENERATED FILE -- 1645172173
|
||||
// clang-format off
|
||||
#pragma once
|
||||
|
||||
@@ -248,6 +248,14 @@ extern "C" const size_t Bun__Path_object_align_ = alignof(Bun__Path);
|
||||
extern "C" const size_t Zig__ConsoleClient_object_size_ = sizeof(Zig::ConsoleClient);
|
||||
extern "C" const size_t Zig__ConsoleClient_object_align_ = alignof(Zig::ConsoleClient);
|
||||
|
||||
#ifndef INCLUDED_
|
||||
#define INCLUDED_
|
||||
#include
|
||||
#endif
|
||||
|
||||
extern "C" const size_t Bun__Timer_object_size_ = sizeof(Bun__Timer);
|
||||
extern "C" const size_t Bun__Timer_object_align_ = alignof(Bun__Timer);
|
||||
|
||||
const size_t sizes[30] = {sizeof(JSC::JSObject), sizeof(SystemError), sizeof(JSC::JSCell), sizeof(JSC::JSString), sizeof(Inspector::ScriptArguments), sizeof(JSC::JSModuleLoader), sizeof(JSC::JSModuleRecord), sizeof(JSC::JSPromise), sizeof(JSC::JSInternalPromise), sizeof(JSC::SourceOrigin), sizeof(JSC::SourceCode), sizeof(JSC::JSFunction), sizeof(JSC::JSGlobalObject), sizeof(WTF::URL), sizeof(WTF::String), sizeof(JSC::JSValue), sizeof(JSC::PropertyName), sizeof(JSC::Exception), sizeof(JSC::VM), sizeof(JSC::ThrowScope), sizeof(JSC::CatchScope), sizeof(JSC::CallFrame), sizeof(JSC::Identifier), sizeof(WTF::StringImpl), sizeof(WTF::ExternalStringImpl), sizeof(WTF::StringView), sizeof(Zig::GlobalObject), sizeof(Bun__Readable), sizeof(Bun__Writable), sizeof(Bun__Path)};
|
||||
|
||||
const char* names[30] = {"JSC__JSObject", "SystemError", "JSC__JSCell", "JSC__JSString", "Inspector__ScriptArguments", "JSC__JSModuleLoader", "JSC__JSModuleRecord", "JSC__JSPromise", "JSC__JSInternalPromise", "JSC__SourceOrigin", "JSC__SourceCode", "JSC__JSFunction", "JSC__JSGlobalObject", "WTF__URL", "WTF__String", "JSC__JSValue", "JSC__PropertyName", "JSC__Exception", "JSC__VM", "JSC__ThrowScope", "JSC__CatchScope", "JSC__CallFrame", "JSC__Identifier", "WTF__StringImpl", "WTF__ExternalStringImpl", "WTF__StringView", "Zig__GlobalObject", "Bun__Readable", "Bun__Writable", "Bun__Path"};
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// clang-format: off
|
||||
//-- AUTOGENERATED FILE -- 1643935973
|
||||
//-- AUTOGENERATED FILE -- 1645172173
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
@@ -722,3 +722,16 @@ ZIG_DECL void Zig__ConsoleClient__timeLog(void* arg0, JSC__JSGlobalObject* arg1,
|
||||
ZIG_DECL void Zig__ConsoleClient__timeStamp(void* arg0, JSC__JSGlobalObject* arg1, Inspector__ScriptArguments* arg2);
|
||||
|
||||
#endif
|
||||
|
||||
#pragma mark - Bun__Timer
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
ZIG_DECL JSC__JSValue Bun__Timer__clearInterval(JSC__JSGlobalObject* arg0, JSC__JSValue JSValue1);
|
||||
ZIG_DECL JSC__JSValue Bun__Timer__clearTimeout(JSC__JSGlobalObject* arg0, JSC__JSValue JSValue1);
|
||||
ZIG_DECL int32_t Bun__Timer__getNextID();
|
||||
ZIG_DECL JSC__JSValue Bun__Timer__setInterval(JSC__JSGlobalObject* arg0, JSC__JSValue JSValue1, JSC__JSValue JSValue2);
|
||||
ZIG_DECL JSC__JSValue Bun__Timer__setTimeout(JSC__JSGlobalObject* arg0, JSC__JSValue JSValue1, JSC__JSValue JSValue2);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user