mirror of
https://github.com/denoland/deno.git
synced 2025-03-03 17:34:47 -05:00
Add deno_dispose to tests.
And fix ArrayBuffer memory problem.
This commit is contained in:
parent
cbbe8ad999
commit
9590c87c62
3 changed files with 15 additions and 8 deletions
|
@ -276,7 +276,7 @@ bool deno_load(Deno* d, const char* name_s, const char* source_s) {
|
||||||
|
|
||||||
// Routes message to the javascript callback set with deno_recv().
|
// Routes message to the javascript callback set with deno_recv().
|
||||||
// False return value indicates error. Check deno_last_exception() for exception
|
// False return value indicates error. Check deno_last_exception() for exception
|
||||||
// text.
|
// text. Caller owns buf.
|
||||||
bool deno_send(Deno* d, deno_buf buf) {
|
bool deno_send(Deno* d, deno_buf buf) {
|
||||||
v8::Locker locker(d->isolate);
|
v8::Locker locker(d->isolate);
|
||||||
v8::Isolate::Scope isolate_scope(d->isolate);
|
v8::Isolate::Scope isolate_scope(d->isolate);
|
||||||
|
@ -287,16 +287,18 @@ bool deno_send(Deno* d, deno_buf buf) {
|
||||||
|
|
||||||
v8::TryCatch try_catch(d->isolate);
|
v8::TryCatch try_catch(d->isolate);
|
||||||
|
|
||||||
v8::Local<v8::Function> recv =
|
auto recv = d->recv.Get(d->isolate);
|
||||||
v8::Local<v8::Function>::New(d->isolate, d->recv);
|
|
||||||
if (recv.IsEmpty()) {
|
if (recv.IsEmpty()) {
|
||||||
d->last_exception = "deno_recv has not been called.";
|
d->last_exception = "deno_recv has not been called.";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO(ry) support zero copy.
|
||||||
|
auto ab = v8::ArrayBuffer::New(d->isolate, buf.len);
|
||||||
|
memcpy(ab->GetContents().Data(), buf.data, buf.len);
|
||||||
|
|
||||||
v8::Local<v8::Value> args[1];
|
v8::Local<v8::Value> args[1];
|
||||||
args[0] = v8::ArrayBuffer::New(d->isolate, buf.data, buf.len,
|
args[0] = ab;
|
||||||
v8::ArrayBufferCreationMode::kInternalized);
|
|
||||||
assert(!args[0].IsEmpty());
|
assert(!args[0].IsEmpty());
|
||||||
assert(!try_catch.HasCaught());
|
assert(!try_catch.HasCaught());
|
||||||
|
|
||||||
|
|
|
@ -1,14 +1,13 @@
|
||||||
// A simple runtime that doesn't involve typescript or protobufs to test
|
// A simple runtime that doesn't involve typescript or protobufs to test
|
||||||
// libdeno.
|
// libdeno.
|
||||||
const globalEval = eval;
|
const window = eval("this");
|
||||||
const window = globalEval("this");
|
|
||||||
window['foo'] = () => {
|
window['foo'] = () => {
|
||||||
deno_print("Hello world from foo");
|
deno_print("Hello world from foo");
|
||||||
return "foo";
|
return "foo";
|
||||||
}
|
}
|
||||||
|
|
||||||
function assert(cond) {
|
function assert(cond) {
|
||||||
if (!cond) throw Error("assert failed");
|
if (!cond) throw Error("mock_runtime.js assert failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
function recvabc() {
|
function recvabc() {
|
||||||
|
|
|
@ -7,16 +7,19 @@
|
||||||
TEST(MockRuntimeTest, InitializesCorrectly) {
|
TEST(MockRuntimeTest, InitializesCorrectly) {
|
||||||
Deno* d = deno_new(NULL, NULL);
|
Deno* d = deno_new(NULL, NULL);
|
||||||
EXPECT_TRUE(deno_load(d, "a.js", "1 + 2"));
|
EXPECT_TRUE(deno_load(d, "a.js", "1 + 2"));
|
||||||
|
deno_dispose(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(MockRuntimeTest, CanCallFoo) {
|
TEST(MockRuntimeTest, CanCallFoo) {
|
||||||
Deno* d = deno_new(NULL, NULL);
|
Deno* d = deno_new(NULL, NULL);
|
||||||
EXPECT_TRUE(deno_load(d, "a.js", "if (foo() != 'foo') throw Error();"));
|
EXPECT_TRUE(deno_load(d, "a.js", "if (foo() != 'foo') throw Error();"));
|
||||||
|
deno_dispose(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(MockRuntimeTest, ErrorsCorrectly) {
|
TEST(MockRuntimeTest, ErrorsCorrectly) {
|
||||||
Deno* d = deno_new(NULL, NULL);
|
Deno* d = deno_new(NULL, NULL);
|
||||||
EXPECT_FALSE(deno_load(d, "a.js", "throw Error()"));
|
EXPECT_FALSE(deno_load(d, "a.js", "throw Error()"));
|
||||||
|
deno_dispose(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
deno_buf strbuf(const char* str) {
|
deno_buf strbuf(const char* str) {
|
||||||
|
@ -28,6 +31,7 @@ TEST(MockRuntimeTest, SendSuccess) {
|
||||||
Deno* d = deno_new(NULL, NULL);
|
Deno* d = deno_new(NULL, NULL);
|
||||||
EXPECT_TRUE(deno_load(d, "a.js", "recvabc();"));
|
EXPECT_TRUE(deno_load(d, "a.js", "recvabc();"));
|
||||||
EXPECT_TRUE(deno_send(d, strbuf("abc")));
|
EXPECT_TRUE(deno_send(d, strbuf("abc")));
|
||||||
|
deno_dispose(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(MockRuntimeTest, SendByteLength) {
|
TEST(MockRuntimeTest, SendByteLength) {
|
||||||
|
@ -35,12 +39,14 @@ TEST(MockRuntimeTest, SendByteLength) {
|
||||||
EXPECT_TRUE(deno_load(d, "a.js", "recvabc();"));
|
EXPECT_TRUE(deno_load(d, "a.js", "recvabc();"));
|
||||||
// We send the wrong sized message, it should throw.
|
// We send the wrong sized message, it should throw.
|
||||||
EXPECT_FALSE(deno_send(d, strbuf("abcd")));
|
EXPECT_FALSE(deno_send(d, strbuf("abcd")));
|
||||||
|
deno_dispose(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(MockRuntimeTest, SendNoCallback) {
|
TEST(MockRuntimeTest, SendNoCallback) {
|
||||||
Deno* d = deno_new(NULL, NULL);
|
Deno* d = deno_new(NULL, NULL);
|
||||||
// We didn't call deno_recv(), sending should fail.
|
// We didn't call deno_recv(), sending should fail.
|
||||||
EXPECT_FALSE(deno_send(d, strbuf("abc")));
|
EXPECT_FALSE(deno_send(d, strbuf("abc")));
|
||||||
|
deno_dispose(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue