From 154422a3d1af51c1b8cb3849c28330cbb283f62f Mon Sep 17 00:00:00 2001 From: Scott Kyle Date: Fri, 26 Feb 2016 12:11:35 -0800 Subject: [PATCH] Make realm instances pass instanceof check --- src/js_realm.cpp | 20 +++++++++++++------- tests/lib/realm-tests.js | 5 +++++ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/js_realm.cpp b/src/js_realm.cpp index 2e80f099..a21ab798 100644 --- a/src/js_realm.cpp +++ b/src/js_realm.cpp @@ -217,16 +217,22 @@ JSObjectRef RealmConstructor(JSContextRef ctx, JSObjectRef constructor, size_t a } } +bool RealmHasInstance(JSContextRef ctx, JSObjectRef constructor, JSValueRef value, JSValueRef* exception) { + return JSValueIsObjectOfClass(ctx, value, RJSRealmClass()); +} + +static const JSStaticValue RealmStaticProperties[] = { + {"defaultPath", GetDefaultPath, SetDefaultPath, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {NULL, NULL} +}; + JSClassRef RJSRealmConstructorClass() { JSClassDefinition realmConstructorDefinition = kJSClassDefinitionEmpty; - realmConstructorDefinition.className = "Realm"; + realmConstructorDefinition.attributes = kJSClassAttributeNoAutomaticPrototype; + realmConstructorDefinition.className = "RealmConstructor"; realmConstructorDefinition.callAsConstructor = RealmConstructor; - - JSStaticValue realmStaticProperties[] = { - {"defaultPath", GetDefaultPath, SetDefaultPath, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, - {NULL, NULL} - }; - realmConstructorDefinition.staticValues = realmStaticProperties; + realmConstructorDefinition.hasInstance = RealmHasInstance; + realmConstructorDefinition.staticValues = RealmStaticProperties; return JSClassCreate(&realmConstructorDefinition); } diff --git a/tests/lib/realm-tests.js b/tests/lib/realm-tests.js index 476219f4..8555d05b 100644 --- a/tests/lib/realm-tests.js +++ b/tests/lib/realm-tests.js @@ -25,6 +25,11 @@ var schemas = require('./schemas'); var util = require('./util'); module.exports = BaseTest.extend({ + testRealmConstructor: function() { + var realm = new Realm({schema: []}); + TestCase.assertTrue(realm instanceof Realm); + }, + testRealmConstructorPath: function() { TestCase.assertThrows(function() { new Realm('/invalidpath');