1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

Add deno_dispose to tests.

And fix ArrayBuffer memory problem.
This commit is contained in:
Ryan Dahl 2018-06-11 19:05:27 +02:00
parent cbbe8ad999
commit 9590c87c62
3 changed files with 15 additions and 8 deletions

View file

@ -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().
// False return value indicates error. Check deno_last_exception() for exception
// text.
// text. Caller owns buf.
bool deno_send(Deno* d, deno_buf buf) {
v8::Locker locker(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::Local<v8::Function> recv =
v8::Local<v8::Function>::New(d->isolate, d->recv);
auto recv = d->recv.Get(d->isolate);
if (recv.IsEmpty()) {
d->last_exception = "deno_recv has not been called.";
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];
args[0] = v8::ArrayBuffer::New(d->isolate, buf.data, buf.len,
v8::ArrayBufferCreationMode::kInternalized);
args[0] = ab;
assert(!args[0].IsEmpty());
assert(!try_catch.HasCaught());

View file

@ -1,14 +1,13 @@
// A simple runtime that doesn't involve typescript or protobufs to test
// libdeno.
const globalEval = eval;
const window = globalEval("this");
const window = eval("this");
window['foo'] = () => {
deno_print("Hello world from foo");
return "foo";
}
function assert(cond) {
if (!cond) throw Error("assert failed");
if (!cond) throw Error("mock_runtime.js assert failed");
}
function recvabc() {

View file

@ -7,16 +7,19 @@
TEST(MockRuntimeTest, InitializesCorrectly) {
Deno* d = deno_new(NULL, NULL);
EXPECT_TRUE(deno_load(d, "a.js", "1 + 2"));
deno_dispose(d);
}
TEST(MockRuntimeTest, CanCallFoo) {
Deno* d = deno_new(NULL, NULL);
EXPECT_TRUE(deno_load(d, "a.js", "if (foo() != 'foo') throw Error();"));
deno_dispose(d);
}
TEST(MockRuntimeTest, ErrorsCorrectly) {
Deno* d = deno_new(NULL, NULL);
EXPECT_FALSE(deno_load(d, "a.js", "throw Error()"));
deno_dispose(d);
}
deno_buf strbuf(const char* str) {
@ -28,6 +31,7 @@ TEST(MockRuntimeTest, SendSuccess) {
Deno* d = deno_new(NULL, NULL);
EXPECT_TRUE(deno_load(d, "a.js", "recvabc();"));
EXPECT_TRUE(deno_send(d, strbuf("abc")));
deno_dispose(d);
}
TEST(MockRuntimeTest, SendByteLength) {
@ -35,12 +39,14 @@ TEST(MockRuntimeTest, SendByteLength) {
EXPECT_TRUE(deno_load(d, "a.js", "recvabc();"));
// We send the wrong sized message, it should throw.
EXPECT_FALSE(deno_send(d, strbuf("abcd")));
deno_dispose(d);
}
TEST(MockRuntimeTest, SendNoCallback) {
Deno* d = deno_new(NULL, NULL);
// We didn't call deno_recv(), sending should fail.
EXPECT_FALSE(deno_send(d, strbuf("abc")));
deno_dispose(d);
}
int main(int argc, char** argv) {