Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ObjectReference test case #212

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions test/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Object InitObject(Env env);
Object InitPromise(Env env);
Object InitTypedArray(Env env);
Object InitObjectWrap(Env env);
Object InitObjectReference(Env env);

Object Init(Env env, Object exports) {
exports.Set("arraybuffer", InitArrayBuffer(env));
Expand All @@ -30,6 +31,7 @@ Object Init(Env env, Object exports) {
exports.Set("promise", InitPromise(env));
exports.Set("typedarray", InitTypedArray(env));
exports.Set("objectwrap", InitObjectWrap(env));
exports.Set("objectreference", InitObjectReference(env));
return exports;
}

Expand Down
1 change: 1 addition & 0 deletions test/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
'promise.cc',
'typedarray.cc',
'objectwrap.cc',
'objectreference.cc',
],
'include_dirs': ["<!@(node -p \"require('../').include\")"],
'dependencies': ["<!(node -p \"require('../').gyp\")"],
Expand Down
3 changes: 2 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ let testModules = [
'object/set_property',
'promise',
'typedarray',
'objectwrap'
'objectwrap',
'objectreference',
];

if (typeof global.gc === 'function') {
Expand Down
163 changes: 163 additions & 0 deletions test/objectreference.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/* ObjectReference cant be used to create references to Values that
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be can as opposed to cant

are not Objects by creating a blank Object and setting Values to
it. Subclasses of Objects can only be set using an ObjectReference
by first casting it as an Object. */

#include "napi.h"

using namespace Napi;

ObjectReference weak;
ObjectReference persistent;
ObjectReference reference;

ObjectReference casted_weak;
ObjectReference casted_persistent;
ObjectReference casted_reference;

void SetObjects(const CallbackInfo& info) {
Env env = info.Env();
HandleScope scope(env);

dummy_weak = Weak(Object::New(env));
dummy_persistent = Persistent(Object::New(env));
dummy_reference = Reference<Object>::New(Object::New(env), 2);

if (info[0].IsString()) {
if (info[2].As<String>() == String::New(env, "javascript")) {
dummy_weak.Set(info[0].As<String>(), info[1]);
dummy_persistent.Set(info[0].As<String>(), info[1]);
unrefDummy();
dummy_reference.Set(info[0].As<String>(), info[1]);
} else {
dummy_weak.Set(info[0].As<String>().Utf8Value(), info[1]);
dummy_persistent.Set(info[0].As<String>().Utf8Value(), info[1]);
dummy_reference.Set(info[0].As<String>().Utf8Value(), info[1]);
}
} else if (info[0].IsNumber()) {
dummy_weak.Set(info[0].As<Number>(), info[1]);
dummy_persistent.Set(info[0].As<Number>(), info[1]);
dummy_reference.Set(info[0].As<Number>(), info[1]);
}
}

void SetCastedObjects(const CallbackInfo& info) {
Env env = info.Env();
HandleScope scope(env);

Array ex = Array::New(env);
ex.Set(Number::New(env, 0), String::New(env, "hello"));
ex.Set(Number::New(env, 1), String::New(env, "world"));
ex.Set(Number::New(env, 2), String::New(env, "!"));

casted_weak = Weak(ex.As<Object>());
casted_persistent = Persistent(ex.As<Object>());
casted_reference = Reference<Object>::New(ex.As<Object>(), 2);
}


Value GetFromValue(const CallbackInfo& info) {
Env env = info.Env();

if (info[0].As<String>() == String::New(env, "weak")) {
if (dummy_weak.IsEmpty()) {
return String::New(env, "No Referenced Value");
} else {
return dummy_weak.Value();
}
} else if (info[0].As<String>() == String::New(env, "persistent")) {
return dummy_persistent.Value();
} else {
return dummy_reference.Value();
}
}

Value GetFromGetter(const CallbackInfo& info) {
Env env = info.Env();

if (info[0].As<String>() == String::New(env, "weak")) {
if (dummy_weak.IsEmpty()) {
return String::New(env, "No Referenced Value");
} else {
if (info[1].IsString()) {
return dummy_weak.Get(info[1].As<String>().Utf8Value());
} else if (info[1].IsNumber()) {
return dummy_weak.Get(info[1].As<Number>().Uint32Value());
}
}
} else if (info[0].As<String>() == String::New(env, "persistent")) {
if (info[1].IsString()) {
return dummy_persistent.Get(info[1].As<String>().Utf8Value());
} else if (info[1].IsNumber()) {
return dummy_persistent.Get(info[1].As<Number>().Uint32Value());
}
} else {
if (info[0].IsString()) {
return dummy_reference.Get(info[0].As<String>().Utf8Value());
} else if (info[0].IsNumber()) {
return dummy_reference.Get(info[0].As<Number>().Uint32Value());
}
}

return String::New(env, "Error: Reached end of getter");
}

Value GetCastedFromValue(const CallbackInfo& info) {
Env env = info.Env();

if (info[0].As<String>() == String::New(env, "weak")) {
if (casted_weak.IsEmpty()) {
return String::New(env, "No Referenced Value");
} else {
return casted_weak.Value();
}
} else if (info[0].As<String>() == String::New(env, "persistent")) {
return casted_persistent.Value();
} else {
return casted_reference.Value();
}
}

Value GetCastedFromGetter(const CallbackInfo& info) {
Env env = info.Env();

if (info[0].As<String>() == String::New(env, "weak")) {
if (casted_weak.IsEmpty()) {
return String::New(env, "No Referenced Value");
} else {
return casted_weak.Get(info[1].As<Number>());
}
} else if (info[0].As<String>() == String::New(env, "persistent")) {
return casted_persistent.Get(info[1].As<Number>());
} else {
return casted_reference.Get(info[1].As<Number>());
}
}

void UnrefObjects(const CallbackInfo& info) {
Env env = info.Env();

if (info[0].As<String>() == String::New(env, "dummy persistent")) {
dummy_persistent.Unref();
} else if (info[0].As<String>() == String::New(env, "dummy references")) {
dummy_reference.Unref();
} else if (info[0].As<String>() == String::New(env, "casted persistent")) {
casted_persistent.Unref();
} else if (info[0].As<String>() == String::New(env, "casted reference")) {
casted_reference.Unref();
}
}

Object InitObjectReference(Env env) {
Object exports = Object::New(env);

exports["setCastedObjects"] = Function::New(env, SetCastedObjects);
exports["setObjects"] = Function::New(env, SetObjects);
exports["getCastedFromValue"] = Function::New(env, GetCastedFromValue);
exports["getFromGetter"] = Function::New(env, GetFromGetter);
exports["getCastedFromGetter"] = Function::New(env, GetCastedFromGetter);
exports["getFromValue"] = Function::New(env, GetFromValue);
exports["unrefObjects"] = Function::New(env, UnrefObjects);

return exports;
}
194 changes: 194 additions & 0 deletions test/objectreference.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
'use strict';
const buildType = process.config.target_defaults.default_configuration;
const assert = require('assert');
const testUtil = require('./testUtil');

test(require(`./build/${buildType}/binding.node`));
test(require(`./build/${buildType}/binding_noexcept.node`));

function test(binding) {
function testCastedEqual(testToCompare) {
var compare_test = ["hello", "world", "!"];
if (testToCompare instanceof Array) {
assert.deepEqual(compare_test, testToCompare);
} else if (testToCompare instanceof String) {
assert.deepEqual("No Referenced Value", testToCompare);
} else {
assert.fail();
}
}

testUtil.runGCTests([
'Weak Casted Array',
() => {
binding.objectreference.setCastedObjects();
var test = binding.objectreference.getCastedFromValue("weak");
var test2 = new Array();
test2[0] = binding.objectreference.getCastedFromGetter("weak", 0);
test2[1] = binding.objectreference.getCastedFromGetter("weak", 1);
test2[2] = binding.objectreference.getCastedFromGetter("weak", 2);

testCastedEqual(test);
testCastedEqual(test2);
},

'Persistent Casted Array',
() => {
binding.objectreference.setCastedObjects();
const test = binding.objectreference.getCastedFromValue("persistent");
const test2 = new Array();
test2[0] = binding.objectreference.getCastedFromGetter("persistent", 0);
test2[1] = binding.objectreference.getCastedFromGetter("persistent", 1);
test2[2] = binding.objectreference.getCastedFromGetter("persistent", 2);

assert.ok(test instanceof Array);
assert.ok(test2 instanceof Array);
testCastedEqual(test);
testCastedEqual(test2);
},

'References Casted Array',
() => {
binding.objectreference.setCastedObjects();
const test = binding.objectreference.getCastedFromValue();
const test2 = new Array();
test2[0] = binding.objectreference.getCastedFromGetter("reference", 0);
test2[1] = binding.objectreference.getCastedFromGetter("reference", 1);
test2[2] = binding.objectreference.getCastedFromGetter("reference", 2);

assert.ok(test instanceof Array);
assert.ok(test2 instanceof Array);
testCastedEqual(test);
testCastedEqual(test2);
},

'Weak Dummy',
() => {
binding.objectreference.setDummyObjects("hello", "world");
const test = binding.objectreference.getDummyFromValue("weak");
const test2 = binding.objectreference.getDummyFromGetter("weak", "hello");

assert.deepEqual({ hello: "world"}, test);
assert.equal("world", test2);
assert.equal(test["hello"], test2);
},
() => {
binding.objectreference.setDummyObjects(1, "hello world");
const test = binding.objectreference.getDummyFromValue("weak");
const test2 = binding.objectreference.getDummyFromGetter("weak", 1);

assert.deepEqual({ 1: "hello world"}, test);
assert.equal("hello world", test2);
assert.equal(test[1], test2);
},
() => {
binding.objectreference.setDummyObjects(0, "hello");
binding.objectreference.setDummyObjects(1, "world");
const test = binding.objectreference.getDummyFromValue("weak");
const test2 = binding.objectreference.getDummyFromGetter("weak", 0);
const test3 = binding.objectreference.getDummyFromGetter("weak", 1);

assert.deepEqual({ 1: "world"}, test);
assert.equal(undefined, test2);
assert.equal("world", test3);
},

'Persistent Dummy',
() => {
binding.objectreference.setDummyObjects("hello", "world");
const test = binding.objectreference.getDummyFromValue("persistent");
const test2 = binding.objectreference.getDummyFromGetter("persistent", "hello");

assert.deepEqual({ hello: "world"}, test);
assert.equal("world", test2);
assert.equal(test["hello"], test2);
},
() => {
binding.objectreference.setDummyObjects(1, "hello world");
const test = binding.objectreference.getDummyFromValue("persistent");
const test2 = binding.objectreference.getDummyFromGetter("persistent", 1);

assert.deepEqual({ 1: "hello world"}, test);
assert.equal("hello world", test2);
assert.equal(test[1], test2);
},
() => {
binding.objectreference.setDummyObjects(0, "hello");
binding.objectreference.setDummyObjects(1, "world");
const test = binding.objectreference.getDummyFromValue("persistent");
const test2 = binding.objectreference.getDummyFromGetter("persistent", 0);
const test3 = binding.objectreference.getDummyFromGetter("persistent", 1);

assert.deepEqual({ 1: "world"}, test);
assert.equal(undefined, test2);
assert.equal("world", test3);
},
() => {
binding.objectreference.setDummyObjects("hello", "world");
assert.doesNotThrow(
() => {
binding.objectreference.unrefObjects("dummy persistent");
},
Error
);
assert.throws(
() => {
binding.objectreference.unrefObjects("dummy persistent");
},
Error
);
},

'References Dummy',
() => {
binding.objectreference.setDummyObjects("hello", "world");
const test = binding.objectreference.getDummyFromValue();
const test2 = binding.objectreference.getDummyFromGetter("hello");

assert.deepEqual({ hello: "world"}, test);
assert.equal("world", test2);
assert.equal(test["hello"], test2);
},
() => {
binding.objectreference.setDummyObjects(1, "hello world");
const test = binding.objectreference.getDummyFromValue();
const test2 = binding.objectreference.getDummyFromGetter(1);

assert.deepEqual({ 1: "hello world"}, test);
assert.equal("hello world", test2);
assert.equal(test[1], test2);
},
() => {
binding.objectreference.setDummyObjects(0, "hello");
binding.objectreference.setDummyObjects(1, "world");
const test = binding.objectreference.getDummyFromValue();
const test2 = binding.objectreference.getDummyFromGetter(0);
const test3 = binding.objectreference.getDummyFromGetter(1);

assert.deepEqual({ 1: "world"}, test);
assert.equal(undefined, test2);
assert.equal("world", test3);
},
() => {
binding.objectreference.setDummyObjects("hello", "world");
assert.doesNotThrow(
() => {
binding.objectreference.unrefObjects("dummy reference");
},
Error
);
assert.doesNotThrow(
() => {
binding.objectreference.unrefObjects("dummy reference");
},
Error
);
assert.throws(
() => {
binding.objectreference.unrefObjects("dummy reference");
},
Error
);
}
])
};