2 * Copyright (C) 2014 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "CustomGlobalObjectClassTest.h"
28 #include <JavaScriptCore/JavaScriptCore.h>
31 extern bool assertTrue(bool value
, const char* message
);
33 static bool executedCallback
= false;
35 static JSValueRef
jsDoSomething(JSContextRef ctx
, JSObjectRef function
, JSObjectRef thisObject
, size_t argc
, const JSValueRef args
[], JSValueRef
* exception
)
42 executedCallback
= true;
43 return JSValueMakeNull(ctx
);
46 static JSStaticFunction bridgedFunctions
[] = {
47 {"doSomething", jsDoSomething
, kJSPropertyAttributeDontDelete
},
51 static JSClassRef bridgedObjectClass
= NULL
;
52 static JSClassDefinition bridgedClassDef
;
54 static JSClassRef
jsClassRef()
56 if (!bridgedObjectClass
) {
57 bridgedClassDef
= kJSClassDefinitionEmpty
;
58 bridgedClassDef
.className
= "BridgedObject";
59 bridgedClassDef
.staticFunctions
= bridgedFunctions
;
60 bridgedObjectClass
= JSClassCreate(&bridgedClassDef
);
62 return bridgedObjectClass
;
65 void customGlobalObjectClassTest()
67 JSClassRef bridgedObjectJsClassRef
= jsClassRef();
68 JSGlobalContextRef globalContext
= JSGlobalContextCreate(bridgedObjectJsClassRef
);
70 JSObjectRef globalObj
= JSContextGetGlobalObject(globalContext
);
72 JSPropertyNameArrayRef propertyNames
= JSObjectCopyPropertyNames(globalContext
, globalObj
);
73 size_t propertyCount
= JSPropertyNameArrayGetCount(propertyNames
);
74 assertTrue(propertyCount
== 1, "Property count == 1");
76 JSStringRef propertyNameRef
= JSPropertyNameArrayGetNameAtIndex(propertyNames
, 0);
77 size_t propertyNameLength
= JSStringGetLength(propertyNameRef
);
78 size_t bufferSize
= sizeof(char) * (propertyNameLength
+ 1);
79 char* buffer
= (char*)malloc(bufferSize
);
80 JSStringGetUTF8CString(propertyNameRef
, buffer
, bufferSize
);
81 buffer
[propertyNameLength
] = '\0';
82 assertTrue(!strncmp(buffer
, "doSomething", propertyNameLength
), "First property name is doSomething");
85 bool hasMethod
= JSObjectHasProperty(globalContext
, globalObj
, propertyNameRef
);
86 assertTrue(hasMethod
, "Property found by name");
88 JSValueRef doSomethingProperty
=
89 JSObjectGetProperty(globalContext
, globalObj
, propertyNameRef
, NULL
);
90 assertTrue(!JSValueIsUndefined(globalContext
, doSomethingProperty
), "Property is defined");
92 bool globalObjectClassMatchesClassRef
= JSValueIsObjectOfClass(globalContext
, globalObj
, bridgedObjectJsClassRef
);
93 assertTrue(globalObjectClassMatchesClassRef
, "Global object is the right class");
95 JSStringRef script
= JSStringCreateWithUTF8CString("doSomething();");
96 JSEvaluateScript(globalContext
, script
, NULL
, NULL
, 1, NULL
);
97 JSStringRelease(script
);
99 assertTrue(executedCallback
, "Executed custom global object callback");