aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-02-18 02:33:49 -0800
committerGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-02-18 02:33:49 -0800
commit17ab2c18ebcbf5051f41269a7d6eaff1c2af9d5a (patch)
tree88847d1ba58f0f1fa26410d0be2aaead04ab904d /src
parent3db341392983c8038c011f20b566afa557b16c3c (diff)
downloadbun-17ab2c18ebcbf5051f41269a7d6eaff1c2af9d5a.tar.gz
bun-17ab2c18ebcbf5051f41269a7d6eaff1c2af9d5a.tar.zst
bun-17ab2c18ebcbf5051f41269a7d6eaff1c2af9d5a.zip
[Bun.js] Add a stub for `setTimeout`, `setInterval`, `clearTImeout`, `clearInterval`
Diffstat (limited to 'src')
-rw-r--r--src/javascript/jsc/bindings/ZigGlobalObject.cpp118
-rw-r--r--src/javascript/jsc/bindings/exports.zig4
-rw-r--r--src/javascript/jsc/bindings/headers-cpp.h10
-rw-r--r--src/javascript/jsc/bindings/headers.h15
4 files changed, 144 insertions, 3 deletions
diff --git a/src/javascript/jsc/bindings/ZigGlobalObject.cpp b/src/javascript/jsc/bindings/ZigGlobalObject.cpp
index 3bd4eccce..75568eb9e 100644
--- a/src/javascript/jsc/bindings/ZigGlobalObject.cpp
+++ b/src/javascript/jsc/bindings/ZigGlobalObject.cpp
@@ -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());
diff --git a/src/javascript/jsc/bindings/exports.zig b/src/javascript/jsc/bindings/exports.zig
index de20758b1..c79ca68d8 100644
--- a/src/javascript/jsc/bindings/exports.zig
+++ b/src/javascript/jsc/bindings/exports.zig
@@ -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);
}
diff --git a/src/javascript/jsc/bindings/headers-cpp.h b/src/javascript/jsc/bindings/headers-cpp.h
index 282b6d0e9..caf82035b 100644
--- a/src/javascript/jsc/bindings/headers-cpp.h
+++ b/src/javascript/jsc/bindings/headers-cpp.h
@@ -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"};
diff --git a/src/javascript/jsc/bindings/headers.h b/src/javascript/jsc/bindings/headers.h
index 8ca9c6e1c..b769c49db 100644
--- a/src/javascript/jsc/bindings/headers.h
+++ b/src/javascript/jsc/bindings/headers.h
@@ -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