diff options
author | 2023-01-17 16:29:08 -0800 | |
---|---|---|
committer | 2023-01-17 16:29:08 -0800 | |
commit | fd0edd7aa086b606d7d8c64f37d3f0cceca7b77b (patch) | |
tree | 67a74398c973f4f57610ee2243112f8be0d14f17 | |
parent | 848658c533afb3f14b1f87591624bdf48ed28979 (diff) | |
download | bun-fd0edd7aa086b606d7d8c64f37d3f0cceca7b77b.tar.gz bun-fd0edd7aa086b606d7d8c64f37d3f0cceca7b77b.tar.zst bun-fd0edd7aa086b606d7d8c64f37d3f0cceca7b77b.zip |
[process] Implement `process.emitWarning`
-rw-r--r-- | src/bun.js/bindings/Process.cpp | 34 | ||||
-rw-r--r-- | test/bun.js/process.test.js | 14 |
2 files changed, 48 insertions, 0 deletions
diff --git a/src/bun.js/bindings/Process.cpp b/src/bun.js/bindings/Process.cpp index 039489ed1..69025f373 100644 --- a/src/bun.js/bindings/Process.cpp +++ b/src/bun.js/bindings/Process.cpp @@ -520,6 +520,37 @@ JSC_DEFINE_HOST_FUNCTION(Process_functionAbort, (JSGlobalObject * globalObject, __builtin_unreachable(); } +JSC_DEFINE_HOST_FUNCTION(Process_emitWarning, (JSGlobalObject * lexicalGlobalObject, CallFrame* callFrame)) +{ + Zig::GlobalObject* globalObject = static_cast<Zig::GlobalObject*>(lexicalGlobalObject); + VM& vm = globalObject->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + + if (callFrame->argumentCount() < 1) { + throwVMError(globalObject, scope, "Not enough arguments"_s); + return JSValue::encode(jsUndefined()); + } + + auto jsArgs = JSValue::encode(callFrame->uncheckedArgument(0)); + + RETURN_IF_EXCEPTION(scope, encodedJSValue()); + + auto* process = jsCast<Process*>(globalObject->processObject()); + + auto ident = Identifier::fromString(vm, "warning"_s); + if (process->wrapped().hasEventListeners(ident)) { + WTF::String str = callFrame->uncheckedArgument(0).toWTFString(globalObject); + JSC::MarkedArgumentBuffer args; + args.append(createError(globalObject, str)); + process->wrapped().emit(ident, args); + return JSValue::encode(jsUndefined()); + } + + Zig__ConsoleClient__messageWithTypeAndLevel(reinterpret_cast<Zig::ConsoleClient*>(globalObject->consoleClient().get())->m_client, static_cast<uint32_t>(MessageType::Log), + static_cast<uint32_t>(MessageLevel::Warning), globalObject, &jsArgs, 1); + return JSValue::encode(jsUndefined()); +} + JSC_DEFINE_CUSTOM_GETTER(Process_lazyArgv0Getter, (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, JSC::PropertyName name)) { JSC::JSObject* thisObject = JSValue::decode(thisValue).getObject(); @@ -699,6 +730,9 @@ void Process::finishCreation(JSC::VM& vm) config->putDirect(vm, JSC::Identifier::fromString(vm, "target_defaults"_s), JSC::constructEmptyObject(globalObject), 0); config->putDirect(vm, JSC::Identifier::fromString(vm, "variables"_s), variables, 0); this->putDirect(vm, JSC::Identifier::fromString(vm, "config"_s), config, 0); + + this->putDirectNativeFunction(vm, globalObject, JSC::Identifier::fromString(this->vm(), "emitWarning"_s), + 1, Process_emitWarning, ImplementationVisibility::Public, NoIntrinsic, 0); } const JSC::ClassInfo Process::s_info = { "Process"_s, &Base::s_info, nullptr, nullptr, diff --git a/test/bun.js/process.test.js b/test/bun.js/process.test.js index 48fc61a7e..befff6cbf 100644 --- a/test/bun.js/process.test.js +++ b/test/bun.js/process.test.js @@ -176,3 +176,17 @@ it("process.config", () => { target_defaults: {}, }); }); + +it("process.emitWarning", () => { + process.emitWarning("-- Testing process.emitWarning --"); + var called = 0; + process.on("warning", (err) => { + called++; + expect(err.message).toBe("-- Testing process.on('warning') --"); + }); + process.emitWarning("-- Testing process.on('warning') --"); + expect(called).toBe(1); + expect(process.off("warning")).toBe(process); + process.emitWarning("-- Testing process.on('warning') --"); + expect(called).toBe(1); +}); |