diff options
Diffstat (limited to 'libdeno')
-rw-r--r-- | libdeno/api.cc | 2 | ||||
-rw-r--r-- | libdeno/binding.cc | 19 | ||||
-rw-r--r-- | libdeno/deno.h | 12 | ||||
-rw-r--r-- | libdeno/internal.h | 4 | ||||
-rw-r--r-- | libdeno/libdeno.d.ts | 40 | ||||
-rw-r--r-- | libdeno/libdeno_test.cc | 2 | ||||
-rw-r--r-- | libdeno/libdeno_test.js | 47 | ||||
-rw-r--r-- | libdeno/modules_test.cc | 6 |
8 files changed, 88 insertions, 44 deletions
diff --git a/libdeno/api.cc b/libdeno/api.cc index ab87382c3..fa1fe92f9 100644 --- a/libdeno/api.cc +++ b/libdeno/api.cc @@ -177,7 +177,7 @@ void deno_respond(Deno* d_, void* user_data, deno_buf buf) { auto recv_ = d->recv_.Get(d->isolate_); if (recv_.IsEmpty()) { - d->last_exception_ = "libdeno.recv_ has not been called."; + d->last_exception_ = "Deno.core.recv has not been called."; return; } diff --git a/libdeno/binding.cc b/libdeno/binding.cc index c291852da..ab633f46d 100644 --- a/libdeno/binding.cc +++ b/libdeno/binding.cc @@ -232,7 +232,7 @@ void Recv(const v8::FunctionCallbackInfo<v8::Value>& args) { v8::HandleScope handle_scope(isolate); if (!d->recv_.IsEmpty()) { - isolate->ThrowException(v8_str("libdeno.recv_ already called.")); + isolate->ThrowException(v8_str("Deno.core.recv already called.")); return; } @@ -485,33 +485,36 @@ void InitializeContext(v8::Isolate* isolate, v8::Local<v8::Context> context) { auto global = context->Global(); auto deno_val = v8::Object::New(isolate); - CHECK(global->Set(context, deno::v8_str("libdeno"), deno_val).FromJust()); + CHECK(global->Set(context, deno::v8_str("Deno"), deno_val).FromJust()); + + auto core_val = v8::Object::New(isolate); + CHECK(deno_val->Set(context, deno::v8_str("core"), core_val).FromJust()); auto print_tmpl = v8::FunctionTemplate::New(isolate, Print); auto print_val = print_tmpl->GetFunction(context).ToLocalChecked(); - CHECK(deno_val->Set(context, deno::v8_str("print"), print_val).FromJust()); + CHECK(core_val->Set(context, deno::v8_str("print"), print_val).FromJust()); auto recv_tmpl = v8::FunctionTemplate::New(isolate, Recv); auto recv_val = recv_tmpl->GetFunction(context).ToLocalChecked(); - CHECK(deno_val->Set(context, deno::v8_str("recv"), recv_val).FromJust()); + CHECK(core_val->Set(context, deno::v8_str("recv"), recv_val).FromJust()); auto send_tmpl = v8::FunctionTemplate::New(isolate, Send); auto send_val = send_tmpl->GetFunction(context).ToLocalChecked(); - CHECK(deno_val->Set(context, deno::v8_str("send"), send_val).FromJust()); + CHECK(core_val->Set(context, deno::v8_str("send"), send_val).FromJust()); auto eval_context_tmpl = v8::FunctionTemplate::New(isolate, EvalContext); auto eval_context_val = eval_context_tmpl->GetFunction(context).ToLocalChecked(); - CHECK(deno_val->Set(context, deno::v8_str("evalContext"), eval_context_val) + CHECK(core_val->Set(context, deno::v8_str("evalContext"), eval_context_val) .FromJust()); auto error_to_json_tmpl = v8::FunctionTemplate::New(isolate, ErrorToJSON); auto error_to_json_val = error_to_json_tmpl->GetFunction(context).ToLocalChecked(); - CHECK(deno_val->Set(context, deno::v8_str("errorToJSON"), error_to_json_val) + CHECK(core_val->Set(context, deno::v8_str("errorToJSON"), error_to_json_val) .FromJust()); - CHECK(deno_val->SetAccessor(context, deno::v8_str("shared"), Shared) + CHECK(core_val->SetAccessor(context, deno::v8_str("shared"), Shared) .FromJust()); } diff --git a/libdeno/deno.h b/libdeno/deno.h index f3902985e..37a302cad 100644 --- a/libdeno/deno.h +++ b/libdeno/deno.h @@ -23,8 +23,8 @@ typedef struct deno_s Deno; // A callback to receive a message from a libdeno.send() javascript call. // control_buf is valid for only for the lifetime of this callback. // data_buf is valid until deno_respond() is called. -typedef void (*deno_recv_cb)(void* user_data, deno_buf control_buf, - deno_buf zerop_copy_buf); +typedef void (*denorecv_cb)(void* user_data, deno_buf control_buf, + deno_buf zerop_copy_buf); void deno_init(); const char* deno_v8_version(); @@ -34,7 +34,7 @@ typedef struct { int will_snapshot; // Default 0. If calling deno_get_snapshot 1. deno_buf load_snapshot; // Optionally: A deno_buf from deno_get_snapshot. deno_buf shared; // Shared buffer to be mapped to libdeno.shared - deno_recv_cb recv_cb; // Maps to libdeno.send() calls. + denorecv_cb recv_cb; // Maps to libdeno.send() calls. } deno_config; // Create a new deno isolate. @@ -57,13 +57,13 @@ void deno_unlock(Deno* d); void deno_execute(Deno* d, void* user_data, const char* js_filename, const char* js_source); -// deno_respond sends up to one message back for every deno_recv_cb made. +// deno_respond sends up to one message back for every denorecv_cb made. // -// If this is called during deno_recv_cb, the issuing libdeno.send() in +// If this is called during denorecv_cb, the issuing libdeno.send() in // javascript will synchronously return the specified buf as an ArrayBuffer (or // null if buf is empty). // -// If this is called after deno_recv_cb has returned, the deno_respond +// If this is called after denorecv_cb has returned, the deno_respond // will call into the JS callback specified by libdeno.recv(). // // (Ideally, but not currently: After calling deno_respond(), the caller no diff --git a/libdeno/internal.h b/libdeno/internal.h index 71cf731b6..54e845965 100644 --- a/libdeno/internal.h +++ b/libdeno/internal.h @@ -104,7 +104,7 @@ class DenoIsolate { const v8::FunctionCallbackInfo<v8::Value>* current_args_; v8::SnapshotCreator* snapshot_creator_; void* global_import_buf_ptr_; - deno_recv_cb recv_cb_; + denorecv_cb recv_cb_; size_t next_zero_copy_id_; void* user_data_; @@ -170,7 +170,7 @@ static intptr_t external_references[] = { static const deno_buf empty_buf = {nullptr, 0, nullptr, 0, 0}; -Deno* NewFromSnapshot(void* user_data, deno_recv_cb cb); +Deno* NewFromSnapshot(void* user_data, denorecv_cb cb); void InitializeContext(v8::Isolate* isolate, v8::Local<v8::Context> context); diff --git a/libdeno/libdeno.d.ts b/libdeno/libdeno.d.ts new file mode 100644 index 000000000..1bc7367d9 --- /dev/null +++ b/libdeno/libdeno.d.ts @@ -0,0 +1,40 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. + +interface EvalErrorInfo { + // Is the object thrown a native Error? + isNativeError: boolean; + // Was the error happened during compilation? + isCompileError: boolean; + // The actual thrown entity + // (might be an Error or anything else thrown by the user) + // If isNativeError is true, this is an Error + // eslint-disable-next-line @typescript-eslint/no-explicit-any + thrown: any; +} + +declare interface MessageCallback { + (msg: Uint8Array): void; +} + +declare interface DenoCore { + recv(cb: MessageCallback): void; + + send( + control: null | ArrayBufferView, + data?: ArrayBufferView + ): null | Uint8Array; + + print(x: string, isErr?: boolean): void; + + shared: SharedArrayBuffer; + + /** Evaluate provided code in the current context. + * It differs from eval(...) in that it does not create a new context. + * Returns an array: [output, errInfo]. + * If an error occurs, `output` becomes null and `errInfo` is non-null. + */ + // eslint-disable-next-line @typescript-eslint/no-explicit-any + evalContext(code: string): [any, EvalErrorInfo | null]; + + errorToJSON: (e: Error) => string; +} diff --git a/libdeno/libdeno_test.cc b/libdeno/libdeno_test.cc index 90fceef73..8949e7f4a 100644 --- a/libdeno/libdeno_test.cc +++ b/libdeno/libdeno_test.cc @@ -320,7 +320,7 @@ TEST(LibDenoTest, SharedAtomics) { deno_buf shared = {nullptr, 0, reinterpret_cast<uint8_t*>(s), sizeof s, 0}; Deno* d = deno_new(deno_config{0, empty, shared, nullptr}); deno_execute(d, nullptr, "a.js", - "Atomics.add(new Int32Array(libdeno.shared), 0, 1)"); + "Atomics.add(new Int32Array(Deno.core.shared), 0, 1)"); EXPECT_EQ(nullptr, deno_last_exception(d)); EXPECT_EQ(s[0], 1); EXPECT_EQ(s[1], 1); diff --git a/libdeno/libdeno_test.js b/libdeno/libdeno_test.js index 8b1ad2e04..b0040025d 100644 --- a/libdeno/libdeno_test.js +++ b/libdeno/libdeno_test.js @@ -10,7 +10,7 @@ function assert(cond) { } global.CanCallFunction = () => { - libdeno.print("Hello world from foo"); + Deno.core.print("Hello world from foo"); return "foo"; }; @@ -28,15 +28,15 @@ global.TypedArraySnapshots = () => { global.RecvReturnEmpty = () => { const m1 = new Uint8Array("abc".split("").map(c => c.charCodeAt(0))); const m2 = m1.slice(); - const r1 = libdeno.send(m1); + const r1 = Deno.core.send(m1); assert(r1 == null); - const r2 = libdeno.send(m2); + const r2 = Deno.core.send(m2); assert(r2 == null); }; global.RecvReturnBar = () => { const m = new Uint8Array("abc".split("").map(c => c.charCodeAt(0))); - const r = libdeno.send(m); + const r = Deno.core.send(m); assert(r instanceof Uint8Array); assert(r.byteLength === 3); const rstr = String.fromCharCode(...r); @@ -44,10 +44,10 @@ global.RecvReturnBar = () => { }; global.DoubleRecvFails = () => { - // libdeno.recv is an internal function and should only be called once from the + // Deno.core.recv is an internal function and should only be called once from the // runtime. - libdeno.recv((channel, msg) => assert(false)); - libdeno.recv((channel, msg) => assert(false)); + Deno.core.recv((channel, msg) => assert(false)); + Deno.core.recv((channel, msg) => assert(false)); }; global.SendRecvSlice = () => { @@ -58,7 +58,7 @@ global.SendRecvSlice = () => { buf[0] = 100 + i; buf[buf.length - 1] = 100 - i; // On the native side, the slice is shortened by 19 bytes. - buf = libdeno.send(buf); + buf = Deno.core.send(buf); assert(buf.byteOffset === i * 11); assert(buf.byteLength === abLen - i * 30 - 19); assert(buf.buffer.byteLength == abLen); @@ -78,17 +78,17 @@ global.JSSendArrayBufferViewTypes = () => { const ab1 = new ArrayBuffer(4321); const u8 = new Uint8Array(ab1, 2468, 1000); u8[0] = 1; - libdeno.send(u8); + Deno.core.send(u8); // Send Uint32Array. const ab2 = new ArrayBuffer(4321); const u32 = new Uint32Array(ab2, 2468, 1000 / Uint32Array.BYTES_PER_ELEMENT); u32[0] = 0x02020202; - libdeno.send(u32); + Deno.core.send(u32); // Send DataView. const ab3 = new ArrayBuffer(4321); const dv = new DataView(ab3, 2468, 1000); dv.setUint8(0, 3); - libdeno.send(dv); + Deno.core.send(dv); }; // The following join has caused SnapshotBug to segfault when using kKeep. @@ -110,7 +110,7 @@ global.ZeroCopyBuf = () => { const b = zeroCopyBuf; // The second parameter of send should modified by the // privileged side. - const r = libdeno.send(a, b); + const r = Deno.core.send(a, b); assert(r == null); // b is different. assert(b[0] === 4); @@ -129,15 +129,15 @@ global.CheckPromiseErrors = () => { try { await fn(); } catch (e) { - libdeno.send(new Uint8Array([42])); + Deno.core.send(new Uint8Array([42])); } })(); }; global.Shared = () => { - const ab = libdeno.shared; + const ab = Deno.core.shared; assert(ab instanceof SharedArrayBuffer); - assert(libdeno.shared != undefined); + assert(Deno.core.shared != undefined); assert(ab.byteLength === 3); const ui8 = new Uint8Array(ab); assert(ui8[0] === 0); @@ -149,45 +149,46 @@ global.Shared = () => { }; global.LibDenoEvalContext = () => { - const [result, errInfo] = libdeno.evalContext("let a = 1; a"); + const [result, errInfo] = Deno.core.evalContext("let a = 1; a"); assert(result === 1); assert(!errInfo); - const [result2, errInfo2] = libdeno.evalContext("a = a + 1; a"); + const [result2, errInfo2] = Deno.core.evalContext("a = a + 1; a"); assert(result2 === 2); assert(!errInfo2); }; global.LibDenoEvalContextError = () => { - const [result, errInfo] = libdeno.evalContext("not_a_variable"); + const [result, errInfo] = Deno.core.evalContext("not_a_variable"); assert(!result); assert(!!errInfo); assert(errInfo.isNativeError); // is a native error (ReferenceError) assert(!errInfo.isCompileError); // is NOT a compilation error assert(errInfo.thrown.message === "not_a_variable is not defined"); - const [result2, errInfo2] = libdeno.evalContext("throw 1"); + const [result2, errInfo2] = Deno.core.evalContext("throw 1"); assert(!result2); assert(!!errInfo2); assert(!errInfo2.isNativeError); // is NOT a native error assert(!errInfo2.isCompileError); // is NOT a compilation error assert(errInfo2.thrown === 1); - const [result3, errInfo3] = - libdeno.evalContext("class AError extends Error {}; throw new AError('e')"); + const [result3, errInfo3] = Deno.core.evalContext( + "class AError extends Error {}; throw new AError('e')" + ); assert(!result3); assert(!!errInfo3); assert(errInfo3.isNativeError); // extend from native error, still native error assert(!errInfo3.isCompileError); // is NOT a compilation error assert(errInfo3.thrown.message === "e"); - const [result4, errInfo4] = libdeno.evalContext("{"); + const [result4, errInfo4] = Deno.core.evalContext("{"); assert(!result4); assert(!!errInfo4); assert(errInfo4.isNativeError); // is a native error (SyntaxError) assert(errInfo4.isCompileError); // is a compilation error! (braces not closed) assert(errInfo4.thrown.message === "Unexpected end of input"); - const [result5, errInfo5] = libdeno.evalContext("eval('{')"); + const [result5, errInfo5] = Deno.core.evalContext("eval('{')"); assert(!result5); assert(!!errInfo5); assert(errInfo5.isNativeError); // is a native error (SyntaxError) diff --git a/libdeno/modules_test.cc b/libdeno/modules_test.cc index 357c48641..9f9228430 100644 --- a/libdeno/modules_test.cc +++ b/libdeno/modules_test.cc @@ -19,7 +19,7 @@ TEST(ModulesTest, Resolution) { static deno_mod a = deno_mod_new(d, true, "a.js", "import { b } from 'b.js'\n" "if (b() != 'b') throw Error();\n" - "libdeno.send(new Uint8Array([4]));"); + "Deno.core.send(new Uint8Array([4]));"); EXPECT_NE(a, 0); EXPECT_EQ(nullptr, deno_last_exception(d)); @@ -71,7 +71,7 @@ TEST(ModulesTest, ResolutionError) { static deno_mod a = deno_mod_new(d, true, "a.js", "import 'bad'\n" - "libdeno.send(new Uint8Array([4]));"); + "Deno.core.send(new Uint8Array([4]));"); EXPECT_NE(a, 0); EXPECT_EQ(nullptr, deno_last_exception(d)); @@ -105,7 +105,7 @@ TEST(ModulesTest, ImportMetaUrl) { static deno_mod a = deno_mod_new(d, true, "a.js", "if ('a.js' != import.meta.url) throw 'hmm'\n" - "libdeno.send(new Uint8Array([4]));"); + "Deno.core.send(new Uint8Array([4]));"); EXPECT_NE(a, 0); EXPECT_EQ(nullptr, deno_last_exception(d)); |