1 | // -*- mode: c++; c-basic-offset: 4 -*-
|
---|
2 | /*
|
---|
3 | * Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | * 1. Redistributions of source code must retain the above copyright
|
---|
9 | * notice, this list of conditions and the following disclaimer.
|
---|
10 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer in the
|
---|
12 | * documentation and/or other materials provided with the distribution.
|
---|
13 | *
|
---|
14 | * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
|
---|
15 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
---|
17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
|
---|
18 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
19 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
---|
20 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
---|
21 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
---|
22 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
24 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
25 | */
|
---|
26 |
|
---|
27 | #include "JavaScriptCore.h"
|
---|
28 | #include <wtf/UnusedParam.h>
|
---|
29 |
|
---|
30 | #if defined(__APPLE__)
|
---|
31 | #include <CoreFoundation/CoreFoundation.h>
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | #include <assert.h>
|
---|
35 | #include <math.h>
|
---|
36 |
|
---|
37 | static JSContextRef context = 0;
|
---|
38 |
|
---|
39 | static void assertEqualsAsBoolean(JSValueRef value, bool expectedValue)
|
---|
40 | {
|
---|
41 | if (JSValueToBoolean(context, value) != expectedValue)
|
---|
42 | fprintf(stderr, "assertEqualsAsBoolean failed: %p, %d\n", value, expectedValue);
|
---|
43 | }
|
---|
44 |
|
---|
45 | static void assertEqualsAsNumber(JSValueRef value, double expectedValue)
|
---|
46 | {
|
---|
47 | double number = JSValueToNumber(context, value);
|
---|
48 | if (number != expectedValue && !(isnan(number) && isnan(expectedValue)))
|
---|
49 | fprintf(stderr, "assertEqualsAsNumber failed: %p, %lf\n", value, expectedValue);
|
---|
50 | }
|
---|
51 |
|
---|
52 | static void assertEqualsAsUTF8String(JSValueRef value, const char* expectedValue)
|
---|
53 | {
|
---|
54 | JSStringBufferRef valueAsString = JSValueCopyStringValue(context, value);
|
---|
55 |
|
---|
56 | size_t jsSize = JSStringBufferGetMaxLengthUTF8(valueAsString);
|
---|
57 | char jsBuffer[jsSize];
|
---|
58 | JSStringBufferGetCharactersUTF8(valueAsString, jsBuffer, jsSize);
|
---|
59 |
|
---|
60 | if (strcmp(jsBuffer, expectedValue) != 0)
|
---|
61 | fprintf(stderr, "assertEqualsAsUTF8String strcmp failed: %s != %s\n", jsBuffer, expectedValue);
|
---|
62 |
|
---|
63 | if (jsSize < strlen(jsBuffer) + 1)
|
---|
64 | fprintf(stderr, "assertEqualsAsUTF8String failed: jsSize was too small\n");
|
---|
65 |
|
---|
66 | JSStringBufferRelease(valueAsString);
|
---|
67 | }
|
---|
68 |
|
---|
69 | #if defined(__APPLE__)
|
---|
70 | static void assertEqualsAsCharactersPtr(JSValueRef value, const char* expectedValue)
|
---|
71 | {
|
---|
72 | JSStringBufferRef valueAsString = JSValueCopyStringValue(context, value);
|
---|
73 |
|
---|
74 | size_t jsLength = JSStringBufferGetLength(valueAsString);
|
---|
75 | const JSChar* jsBuffer = JSStringBufferGetCharactersPtr(valueAsString);
|
---|
76 |
|
---|
77 | CFStringRef expectedValueAsCFString = CFStringCreateWithCString(kCFAllocatorDefault,
|
---|
78 | expectedValue,
|
---|
79 | kCFStringEncodingUTF8);
|
---|
80 | CFIndex cfLength = CFStringGetLength(expectedValueAsCFString);
|
---|
81 | UniChar cfBuffer[cfLength];
|
---|
82 | CFStringGetCharacters(expectedValueAsCFString, CFRangeMake(0, cfLength), cfBuffer);
|
---|
83 | CFRelease(expectedValueAsCFString);
|
---|
84 |
|
---|
85 | if (memcmp(jsBuffer, cfBuffer, cfLength * sizeof(UniChar)) != 0)
|
---|
86 | fprintf(stderr, "assertEqualsAsCharactersPtr failed: jsBuffer != cfBuffer\n");
|
---|
87 |
|
---|
88 | if (jsLength != (size_t)cfLength)
|
---|
89 | fprintf(stderr, "assertEqualsAsCharactersPtr failed: jsLength(%ld) != cfLength(%ld)\n", jsLength, cfLength);
|
---|
90 |
|
---|
91 | JSStringBufferRelease(valueAsString);
|
---|
92 | }
|
---|
93 |
|
---|
94 | static void assertEqualsAsCharacters(JSValueRef value, const char* expectedValue)
|
---|
95 | {
|
---|
96 | JSStringBufferRef valueAsString = JSValueCopyStringValue(context, value);
|
---|
97 |
|
---|
98 | size_t jsLength = JSStringBufferGetLength(valueAsString);
|
---|
99 | JSChar jsBuffer[jsLength];
|
---|
100 | JSStringBufferGetCharacters(valueAsString, jsBuffer, jsLength);
|
---|
101 |
|
---|
102 | CFStringRef expectedValueAsCFString = CFStringCreateWithCString(kCFAllocatorDefault,
|
---|
103 | expectedValue,
|
---|
104 | kCFStringEncodingUTF8);
|
---|
105 | CFIndex cfLength = CFStringGetLength(expectedValueAsCFString);
|
---|
106 | UniChar cfBuffer[cfLength];
|
---|
107 | CFStringGetCharacters(expectedValueAsCFString, CFRangeMake(0, cfLength), cfBuffer);
|
---|
108 | CFRelease(expectedValueAsCFString);
|
---|
109 |
|
---|
110 | if (memcmp(jsBuffer, cfBuffer, cfLength * sizeof(UniChar)) != 0)
|
---|
111 | fprintf(stderr, "assertEqualsAsCharacters failed: jsBuffer != cfBuffer\n");
|
---|
112 |
|
---|
113 | if (jsLength != (size_t)cfLength)
|
---|
114 | fprintf(stderr, "assertEqualsAsCharacters failed: jsLength(%ld) != cfLength(%ld)\n", jsLength, cfLength);
|
---|
115 |
|
---|
116 | JSStringBufferRelease(valueAsString);
|
---|
117 | }
|
---|
118 | #endif // __APPLE__
|
---|
119 |
|
---|
120 | static JSValueRef jsGlobalValue; // non-stack value for testing JSGCProtect()
|
---|
121 |
|
---|
122 | /* MyObject pseudo-class */
|
---|
123 |
|
---|
124 | static bool didInitialize = false;
|
---|
125 | static void MyObject_initialize(JSObjectRef object)
|
---|
126 | {
|
---|
127 | UNUSED_PARAM(context);
|
---|
128 | UNUSED_PARAM(object);
|
---|
129 | didInitialize = true;
|
---|
130 | }
|
---|
131 |
|
---|
132 | static bool MyObject_hasProperty(JSObjectRef object, JSStringBufferRef propertyName)
|
---|
133 | {
|
---|
134 | UNUSED_PARAM(object);
|
---|
135 |
|
---|
136 | if (JSStringBufferIsEqualUTF8(propertyName, "alwaysOne")
|
---|
137 | || JSStringBufferIsEqualUTF8(propertyName, "cantFind")
|
---|
138 | || JSStringBufferIsEqualUTF8(propertyName, "myPropertyName")) {
|
---|
139 | return true;
|
---|
140 | }
|
---|
141 |
|
---|
142 | return false;
|
---|
143 | }
|
---|
144 |
|
---|
145 | static bool MyObject_getProperty(JSContextRef context, JSObjectRef object, JSStringBufferRef propertyName, JSValueRef* returnValue)
|
---|
146 | {
|
---|
147 | UNUSED_PARAM(context);
|
---|
148 | UNUSED_PARAM(object);
|
---|
149 |
|
---|
150 | if (JSStringBufferIsEqualUTF8(propertyName, "alwaysOne")) {
|
---|
151 | *returnValue = JSNumberMake(1);
|
---|
152 | return true;
|
---|
153 | }
|
---|
154 |
|
---|
155 | if (JSStringBufferIsEqualUTF8(propertyName, "myPropertyName")) {
|
---|
156 | *returnValue = JSNumberMake(1);
|
---|
157 | return true;
|
---|
158 | }
|
---|
159 |
|
---|
160 | if (JSStringBufferIsEqualUTF8(propertyName, "cantFind")) {
|
---|
161 | *returnValue = JSUndefinedMake();
|
---|
162 | return true;
|
---|
163 | }
|
---|
164 |
|
---|
165 | return false;
|
---|
166 | }
|
---|
167 |
|
---|
168 | static bool MyObject_setProperty(JSObjectRef object, JSStringBufferRef propertyName, JSValueRef value)
|
---|
169 | {
|
---|
170 | UNUSED_PARAM(object);
|
---|
171 | UNUSED_PARAM(value);
|
---|
172 |
|
---|
173 | if (JSStringBufferIsEqualUTF8(propertyName, "cantSet"))
|
---|
174 | return true; // pretend we set the property in order to swallow it
|
---|
175 |
|
---|
176 | return false;
|
---|
177 | }
|
---|
178 |
|
---|
179 | static bool MyObject_deleteProperty(JSObjectRef object, JSStringBufferRef propertyName)
|
---|
180 | {
|
---|
181 | UNUSED_PARAM(object);
|
---|
182 |
|
---|
183 | if (JSStringBufferIsEqualUTF8(propertyName, "cantDelete"))
|
---|
184 | return true;
|
---|
185 |
|
---|
186 | return false;
|
---|
187 | }
|
---|
188 |
|
---|
189 | static void MyObject_getPropertyList(JSObjectRef object, JSPropertyListRef propertyList)
|
---|
190 | {
|
---|
191 | JSStringBufferRef propertyNameBuf;
|
---|
192 |
|
---|
193 | propertyNameBuf = JSStringBufferCreateUTF8("alwaysOne");
|
---|
194 | JSPropertyListAdd(propertyList, object, propertyNameBuf);
|
---|
195 | JSStringBufferRelease(propertyNameBuf);
|
---|
196 |
|
---|
197 | propertyNameBuf = JSStringBufferCreateUTF8("myPropertyName");
|
---|
198 | JSPropertyListAdd(propertyList, object, propertyNameBuf);
|
---|
199 | JSStringBufferRelease(propertyNameBuf);
|
---|
200 | }
|
---|
201 |
|
---|
202 | static JSValueRef MyObject_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argc, JSValueRef argv[])
|
---|
203 | {
|
---|
204 | UNUSED_PARAM(context);
|
---|
205 | UNUSED_PARAM(object);
|
---|
206 | UNUSED_PARAM(thisObject);
|
---|
207 |
|
---|
208 | if (argc > 0 && JSValueIsStrictEqual(context, argv[0], JSNumberMake(0)))
|
---|
209 | return JSNumberMake(1);
|
---|
210 |
|
---|
211 | return JSUndefinedMake();
|
---|
212 | }
|
---|
213 |
|
---|
214 | static JSObjectRef MyObject_callAsConstructor(JSContextRef context, JSObjectRef object, size_t argc, JSValueRef argv[])
|
---|
215 | {
|
---|
216 | UNUSED_PARAM(context);
|
---|
217 | UNUSED_PARAM(object);
|
---|
218 |
|
---|
219 | if (argc > 0 && JSValueIsStrictEqual(context, argv[0], JSNumberMake(0)))
|
---|
220 | return JSValueToObject(context, JSNumberMake(1));
|
---|
221 |
|
---|
222 | return JSValueToObject(context, JSNumberMake(0));
|
---|
223 | }
|
---|
224 |
|
---|
225 | static bool MyObject_convertToType(JSObjectRef object, JSTypeCode typeCode, JSValueRef* returnValue)
|
---|
226 | {
|
---|
227 | UNUSED_PARAM(object);
|
---|
228 |
|
---|
229 | switch (typeCode) {
|
---|
230 | case kJSTypeBoolean:
|
---|
231 | *returnValue = JSBooleanMake(false); // default object conversion is 'true'
|
---|
232 | return true;
|
---|
233 | case kJSTypeNumber:
|
---|
234 | *returnValue = JSNumberMake(1);
|
---|
235 | return true;
|
---|
236 | default:
|
---|
237 | break;
|
---|
238 | }
|
---|
239 |
|
---|
240 | // string
|
---|
241 | return false;
|
---|
242 | }
|
---|
243 |
|
---|
244 | static bool didFinalize = false;
|
---|
245 | static void MyObject_finalize(JSObjectRef object)
|
---|
246 | {
|
---|
247 | UNUSED_PARAM(context);
|
---|
248 | UNUSED_PARAM(object);
|
---|
249 | didFinalize = true;
|
---|
250 | }
|
---|
251 |
|
---|
252 | JSObjectCallbacks MyObject_callbacks = {
|
---|
253 | 0,
|
---|
254 | &MyObject_initialize,
|
---|
255 | &MyObject_finalize,
|
---|
256 | &MyObject_hasProperty,
|
---|
257 | &MyObject_getProperty,
|
---|
258 | &MyObject_setProperty,
|
---|
259 | &MyObject_deleteProperty,
|
---|
260 | &MyObject_getPropertyList,
|
---|
261 | &MyObject_callAsFunction,
|
---|
262 | &MyObject_callAsConstructor,
|
---|
263 | &MyObject_convertToType,
|
---|
264 | };
|
---|
265 |
|
---|
266 | static JSClassRef MyObject_class(JSContextRef context)
|
---|
267 | {
|
---|
268 | static JSClassRef jsClass;
|
---|
269 | if (!jsClass) {
|
---|
270 | jsClass = JSClassCreate(context, NULL, NULL, &MyObject_callbacks, NULL);
|
---|
271 | }
|
---|
272 |
|
---|
273 | return jsClass;
|
---|
274 | }
|
---|
275 |
|
---|
276 | static JSValueRef print_callAsFunction(JSContextRef context, JSObjectRef functionObject, JSObjectRef thisObject, size_t argc, JSValueRef argv[])
|
---|
277 | {
|
---|
278 | UNUSED_PARAM(functionObject);
|
---|
279 | UNUSED_PARAM(thisObject);
|
---|
280 |
|
---|
281 | if (argc > 0) {
|
---|
282 | JSStringBufferRef string = JSValueCopyStringValue(context, argv[0]);
|
---|
283 | size_t sizeUTF8 = JSStringBufferGetMaxLengthUTF8(string);
|
---|
284 | char stringUTF8[sizeUTF8];
|
---|
285 | JSStringBufferGetCharactersUTF8(string, stringUTF8, sizeUTF8);
|
---|
286 | printf("%s\n", stringUTF8);
|
---|
287 | JSStringBufferRelease(string);
|
---|
288 | }
|
---|
289 |
|
---|
290 | return JSUndefinedMake();
|
---|
291 | }
|
---|
292 |
|
---|
293 | static JSObjectRef myConstructor_callAsConstructor(JSContextRef context, JSObjectRef constructorObject, size_t argc, JSValueRef argv[])
|
---|
294 | {
|
---|
295 | UNUSED_PARAM(constructorObject);
|
---|
296 |
|
---|
297 | JSObjectRef result = JSObjectMake(context, NULL, 0);
|
---|
298 | if (argc > 0) {
|
---|
299 | JSStringBufferRef valueBuffer = JSStringBufferCreateUTF8("value");
|
---|
300 | JSObjectSetProperty(context, result, valueBuffer, argv[0], kJSPropertyAttributeNone);
|
---|
301 | JSStringBufferRelease(valueBuffer);
|
---|
302 | }
|
---|
303 |
|
---|
304 | return result;
|
---|
305 | }
|
---|
306 |
|
---|
307 | static char* createStringWithContentsOfFile(const char* fileName);
|
---|
308 |
|
---|
309 | int main(int argc, char* argv[])
|
---|
310 | {
|
---|
311 | UNUSED_PARAM(argc);
|
---|
312 | UNUSED_PARAM(argv);
|
---|
313 |
|
---|
314 | context = JSContextCreate(NULL, NULL);
|
---|
315 |
|
---|
316 | JSValueRef jsUndefined = JSUndefinedMake();
|
---|
317 | JSValueRef jsNull = JSNullMake();
|
---|
318 | JSValueRef jsTrue = JSBooleanMake(true);
|
---|
319 | JSValueRef jsFalse = JSBooleanMake(false);
|
---|
320 | JSValueRef jsZero = JSNumberMake(0);
|
---|
321 | JSValueRef jsOne = JSNumberMake(1);
|
---|
322 | JSValueRef jsOneThird = JSNumberMake(1.0 / 3.0);
|
---|
323 | JSObjectRef jsObjectNoProto = JSObjectMake(context, NULL, JSNullMake());
|
---|
324 |
|
---|
325 | // FIXME: test funny utf8 characters
|
---|
326 | JSStringBufferRef jsEmptyStringBuf = JSStringBufferCreateUTF8("");
|
---|
327 | JSValueRef jsEmptyString = JSStringMake(jsEmptyStringBuf);
|
---|
328 |
|
---|
329 | JSStringBufferRef jsOneStringBuf = JSStringBufferCreateUTF8("1");
|
---|
330 | JSValueRef jsOneString = JSStringMake(jsOneStringBuf);
|
---|
331 |
|
---|
332 | #if defined(__APPLE__)
|
---|
333 | UniChar singleUniChar = 65; // Capital A
|
---|
334 | CFMutableStringRef cfString =
|
---|
335 | CFStringCreateMutableWithExternalCharactersNoCopy(kCFAllocatorDefault,
|
---|
336 | &singleUniChar,
|
---|
337 | 1,
|
---|
338 | 1,
|
---|
339 | kCFAllocatorNull);
|
---|
340 |
|
---|
341 | JSStringBufferRef jsCFStringBuf = JSStringBufferCreateWithCFString(cfString);
|
---|
342 | JSValueRef jsCFString = JSStringMake(jsCFStringBuf);
|
---|
343 |
|
---|
344 | CFStringRef cfEmptyString = CFStringCreateWithCString(kCFAllocatorDefault, "", kCFStringEncodingUTF8);
|
---|
345 |
|
---|
346 | JSStringBufferRef jsCFEmptyStringBuf = JSStringBufferCreateWithCFString(cfEmptyString);
|
---|
347 | JSValueRef jsCFEmptyString = JSStringMake(jsCFEmptyStringBuf);
|
---|
348 |
|
---|
349 | CFIndex cfStringLength = CFStringGetLength(cfString);
|
---|
350 | UniChar buffer[cfStringLength];
|
---|
351 | CFStringGetCharacters(cfString,
|
---|
352 | CFRangeMake(0, cfStringLength),
|
---|
353 | buffer);
|
---|
354 | JSStringBufferRef jsCFStringWithCharactersBuf = JSStringBufferCreate(buffer, cfStringLength);
|
---|
355 | JSValueRef jsCFStringWithCharacters = JSStringMake(jsCFStringWithCharactersBuf);
|
---|
356 |
|
---|
357 | JSStringBufferRef jsCFEmptyStringWithCharactersBuf = JSStringBufferCreate(buffer, CFStringGetLength(cfEmptyString));
|
---|
358 | JSValueRef jsCFEmptyStringWithCharacters = JSStringMake(jsCFEmptyStringWithCharactersBuf);
|
---|
359 | #endif // __APPLE__
|
---|
360 |
|
---|
361 | assert(JSValueGetType(jsUndefined) == kJSTypeUndefined);
|
---|
362 | assert(JSValueGetType(jsNull) == kJSTypeNull);
|
---|
363 | assert(JSValueGetType(jsTrue) == kJSTypeBoolean);
|
---|
364 | assert(JSValueGetType(jsFalse) == kJSTypeBoolean);
|
---|
365 | assert(JSValueGetType(jsZero) == kJSTypeNumber);
|
---|
366 | assert(JSValueGetType(jsOne) == kJSTypeNumber);
|
---|
367 | assert(JSValueGetType(jsOneThird) == kJSTypeNumber);
|
---|
368 | assert(JSValueGetType(jsEmptyString) == kJSTypeString);
|
---|
369 | assert(JSValueGetType(jsOneString) == kJSTypeString);
|
---|
370 | #if defined(__APPLE__)
|
---|
371 | assert(JSValueGetType(jsCFString) == kJSTypeString);
|
---|
372 | assert(JSValueGetType(jsCFStringWithCharacters) == kJSTypeString);
|
---|
373 | assert(JSValueGetType(jsCFEmptyString) == kJSTypeString);
|
---|
374 | assert(JSValueGetType(jsCFEmptyStringWithCharacters) == kJSTypeString);
|
---|
375 | #endif // __APPLE__
|
---|
376 |
|
---|
377 | // Conversions that throw exceptions
|
---|
378 | assert(NULL == JSValueToObject(context, jsNull));
|
---|
379 | assert(!JSContextGetException(context));
|
---|
380 | assert(isnan(JSValueToNumber(context, jsObjectNoProto)));
|
---|
381 | assert(!JSContextGetException(context));
|
---|
382 | assertEqualsAsCharactersPtr(jsObjectNoProto, "");
|
---|
383 | assert(!JSContextGetException(context));
|
---|
384 |
|
---|
385 | assertEqualsAsBoolean(jsUndefined, false);
|
---|
386 | assertEqualsAsBoolean(jsNull, false);
|
---|
387 | assertEqualsAsBoolean(jsTrue, true);
|
---|
388 | assertEqualsAsBoolean(jsFalse, false);
|
---|
389 | assertEqualsAsBoolean(jsZero, false);
|
---|
390 | assertEqualsAsBoolean(jsOne, true);
|
---|
391 | assertEqualsAsBoolean(jsOneThird, true);
|
---|
392 | assertEqualsAsBoolean(jsEmptyString, false);
|
---|
393 | assertEqualsAsBoolean(jsOneString, true);
|
---|
394 | #if defined(__APPLE__)
|
---|
395 | assertEqualsAsBoolean(jsCFString, true);
|
---|
396 | assertEqualsAsBoolean(jsCFStringWithCharacters, true);
|
---|
397 | assertEqualsAsBoolean(jsCFEmptyString, false);
|
---|
398 | assertEqualsAsBoolean(jsCFEmptyStringWithCharacters, false);
|
---|
399 | #endif // __APPLE__
|
---|
400 |
|
---|
401 | assertEqualsAsNumber(jsUndefined, nan(""));
|
---|
402 | assertEqualsAsNumber(jsNull, 0);
|
---|
403 | assertEqualsAsNumber(jsTrue, 1);
|
---|
404 | assertEqualsAsNumber(jsFalse, 0);
|
---|
405 | assertEqualsAsNumber(jsZero, 0);
|
---|
406 | assertEqualsAsNumber(jsOne, 1);
|
---|
407 | assertEqualsAsNumber(jsOneThird, 1.0 / 3.0);
|
---|
408 | assertEqualsAsNumber(jsEmptyString, 0);
|
---|
409 | assertEqualsAsNumber(jsOneString, 1);
|
---|
410 | #if defined(__APPLE__)
|
---|
411 | assertEqualsAsNumber(jsCFString, nan(""));
|
---|
412 | assertEqualsAsNumber(jsCFStringWithCharacters, nan(""));
|
---|
413 | assertEqualsAsNumber(jsCFEmptyString, 0);
|
---|
414 | assertEqualsAsNumber(jsCFEmptyStringWithCharacters, 0);
|
---|
415 | assert(sizeof(JSChar) == sizeof(UniChar));
|
---|
416 | #endif // __APPLE__
|
---|
417 |
|
---|
418 | assertEqualsAsCharactersPtr(jsUndefined, "undefined");
|
---|
419 | assertEqualsAsCharactersPtr(jsNull, "null");
|
---|
420 | assertEqualsAsCharactersPtr(jsTrue, "true");
|
---|
421 | assertEqualsAsCharactersPtr(jsFalse, "false");
|
---|
422 | assertEqualsAsCharactersPtr(jsZero, "0");
|
---|
423 | assertEqualsAsCharactersPtr(jsOne, "1");
|
---|
424 | assertEqualsAsCharactersPtr(jsOneThird, "0.3333333333333333");
|
---|
425 | assertEqualsAsCharactersPtr(jsEmptyString, "");
|
---|
426 | assertEqualsAsCharactersPtr(jsOneString, "1");
|
---|
427 | #if defined(__APPLE__)
|
---|
428 | assertEqualsAsCharactersPtr(jsCFString, "A");
|
---|
429 | assertEqualsAsCharactersPtr(jsCFStringWithCharacters, "A");
|
---|
430 | assertEqualsAsCharactersPtr(jsCFEmptyString, "");
|
---|
431 | assertEqualsAsCharactersPtr(jsCFEmptyStringWithCharacters, "");
|
---|
432 | #endif // __APPLE__
|
---|
433 |
|
---|
434 | assertEqualsAsCharacters(jsUndefined, "undefined");
|
---|
435 | assertEqualsAsCharacters(jsNull, "null");
|
---|
436 | assertEqualsAsCharacters(jsTrue, "true");
|
---|
437 | assertEqualsAsCharacters(jsFalse, "false");
|
---|
438 | assertEqualsAsCharacters(jsZero, "0");
|
---|
439 | assertEqualsAsCharacters(jsOne, "1");
|
---|
440 | assertEqualsAsCharacters(jsOneThird, "0.3333333333333333");
|
---|
441 | assertEqualsAsCharacters(jsEmptyString, "");
|
---|
442 | assertEqualsAsCharacters(jsOneString, "1");
|
---|
443 | #if defined(__APPLE__)
|
---|
444 | assertEqualsAsCharacters(jsCFString, "A");
|
---|
445 | assertEqualsAsCharacters(jsCFStringWithCharacters, "A");
|
---|
446 | assertEqualsAsCharacters(jsCFEmptyString, "");
|
---|
447 | assertEqualsAsCharacters(jsCFEmptyStringWithCharacters, "");
|
---|
448 | #endif // __APPLE__
|
---|
449 |
|
---|
450 | assertEqualsAsUTF8String(jsUndefined, "undefined");
|
---|
451 | assertEqualsAsUTF8String(jsNull, "null");
|
---|
452 | assertEqualsAsUTF8String(jsTrue, "true");
|
---|
453 | assertEqualsAsUTF8String(jsFalse, "false");
|
---|
454 | assertEqualsAsUTF8String(jsZero, "0");
|
---|
455 | assertEqualsAsUTF8String(jsOne, "1");
|
---|
456 | assertEqualsAsUTF8String(jsOneThird, "0.3333333333333333");
|
---|
457 | assertEqualsAsUTF8String(jsEmptyString, "");
|
---|
458 | assertEqualsAsUTF8String(jsOneString, "1");
|
---|
459 | #if defined(__APPLE__)
|
---|
460 | assertEqualsAsUTF8String(jsCFString, "A");
|
---|
461 | assertEqualsAsUTF8String(jsCFStringWithCharacters, "A");
|
---|
462 | assertEqualsAsUTF8String(jsCFEmptyString, "");
|
---|
463 | assertEqualsAsUTF8String(jsCFEmptyStringWithCharacters, "");
|
---|
464 | #endif // __APPLE__
|
---|
465 |
|
---|
466 | assert(JSValueIsStrictEqual(context, jsTrue, jsTrue));
|
---|
467 | assert(!JSValueIsStrictEqual(context, jsOne, jsOneString));
|
---|
468 |
|
---|
469 | assert(JSValueIsEqual(context, jsOne, jsOneString));
|
---|
470 | assert(!JSValueIsEqual(context, jsTrue, jsFalse));
|
---|
471 |
|
---|
472 | #if defined(__APPLE__)
|
---|
473 | CFStringRef cfJSString = CFStringCreateWithJSStringBuffer(kCFAllocatorDefault, jsCFStringBuf);
|
---|
474 | CFStringRef cfJSEmptyString = CFStringCreateWithJSStringBuffer(kCFAllocatorDefault, jsCFEmptyStringBuf);
|
---|
475 | assert(CFEqual(cfJSString, cfString));
|
---|
476 | assert(CFEqual(cfJSEmptyString, cfEmptyString));
|
---|
477 | CFRelease(cfJSString);
|
---|
478 | CFRelease(cfJSEmptyString);
|
---|
479 |
|
---|
480 | CFRelease(cfString);
|
---|
481 | CFRelease(cfEmptyString);
|
---|
482 | #endif // __APPLE__
|
---|
483 |
|
---|
484 | jsGlobalValue = JSObjectMake(context, NULL, NULL);
|
---|
485 | JSGCProtect(jsGlobalValue);
|
---|
486 | JSGCCollect();
|
---|
487 | assert(JSValueIsObject(jsGlobalValue));
|
---|
488 | JSGCUnprotect(jsGlobalValue);
|
---|
489 |
|
---|
490 | /* JSInterpreter.h */
|
---|
491 |
|
---|
492 | JSObjectRef globalObject = JSContextGetGlobalObject(context);
|
---|
493 | assert(JSValueIsObject(globalObject));
|
---|
494 |
|
---|
495 | JSStringBufferRef goodSyntaxBuf = JSStringBufferCreateUTF8("x = 1;");
|
---|
496 | JSStringBufferRef badSyntaxBuf = JSStringBufferCreateUTF8("x := 1;");
|
---|
497 | assert(JSCheckSyntax(context, goodSyntaxBuf, NULL, 0, NULL));
|
---|
498 | assert(!JSCheckSyntax(context, badSyntaxBuf, NULL, 0, NULL));
|
---|
499 |
|
---|
500 | JSValueRef result;
|
---|
501 | JSValueRef exception;
|
---|
502 |
|
---|
503 | result = JSEvaluate(context, goodSyntaxBuf, NULL, NULL, 1, NULL);
|
---|
504 | assert(result);
|
---|
505 | assert(JSValueIsEqual(context, result, jsOne));
|
---|
506 |
|
---|
507 | result = JSEvaluate(context, badSyntaxBuf, NULL, NULL, 1, &exception);
|
---|
508 | assert(!result);
|
---|
509 | assert(!JSContextGetException(context));
|
---|
510 | assert(JSValueIsObject(exception));
|
---|
511 |
|
---|
512 | JSContextSetException(context, JSNumberMake(1));
|
---|
513 | assert(JSContextGetException(context));
|
---|
514 | assert(JSValueIsEqual(context, jsOne, JSContextGetException(context)));
|
---|
515 | JSContextClearException(context);
|
---|
516 | assert(!JSContextGetException(context));
|
---|
517 |
|
---|
518 | JSStringBufferRelease(jsEmptyStringBuf);
|
---|
519 | JSStringBufferRelease(jsOneStringBuf);
|
---|
520 | #if defined(__APPLE__)
|
---|
521 | JSStringBufferRelease(jsCFStringBuf);
|
---|
522 | JSStringBufferRelease(jsCFEmptyStringBuf);
|
---|
523 | JSStringBufferRelease(jsCFStringWithCharactersBuf);
|
---|
524 | JSStringBufferRelease(jsCFEmptyStringWithCharactersBuf);
|
---|
525 | #endif // __APPLE__
|
---|
526 | JSStringBufferRelease(goodSyntaxBuf);
|
---|
527 | JSStringBufferRelease(badSyntaxBuf);
|
---|
528 |
|
---|
529 | JSStringBufferRef arrayBuf = JSStringBufferCreateUTF8("Array");
|
---|
530 | JSValueRef v;
|
---|
531 | assert(JSObjectGetProperty(context, globalObject, arrayBuf, &v));
|
---|
532 | JSObjectRef arrayConstructor = JSValueToObject(context, v);
|
---|
533 | JSStringBufferRelease(arrayBuf);
|
---|
534 | result = JSObjectCallAsConstructor(context, arrayConstructor, 0, NULL, NULL);
|
---|
535 | assert(result);
|
---|
536 | assert(JSValueIsInstanceOf(context, result, arrayConstructor));
|
---|
537 | assert(!JSValueIsInstanceOf(context, JSNullMake(), arrayConstructor));
|
---|
538 |
|
---|
539 | JSStringBufferRef functionBuf;
|
---|
540 |
|
---|
541 | functionBuf = JSStringBufferCreateUTF8("rreturn Array;");
|
---|
542 | assert(!JSFunctionMakeWithBody(context, functionBuf, NULL, 1));
|
---|
543 | JSStringBufferRelease(functionBuf);
|
---|
544 |
|
---|
545 | functionBuf = JSStringBufferCreateUTF8("return Array;");
|
---|
546 | JSObjectRef function = JSFunctionMakeWithBody(context, functionBuf, NULL, 1);
|
---|
547 | JSStringBufferRelease(functionBuf);
|
---|
548 |
|
---|
549 | assert(JSObjectIsFunction(function));
|
---|
550 | v = JSObjectCallAsFunction(context, function, NULL, 0, NULL, NULL);
|
---|
551 | assert(JSValueIsEqual(context, v, arrayConstructor));
|
---|
552 |
|
---|
553 | JSObjectRef myObject = JSObjectMake(context, MyObject_class(context), NULL);
|
---|
554 | assert(didInitialize);
|
---|
555 | JSStringBufferRef myObjectBuf = JSStringBufferCreateUTF8("MyObject");
|
---|
556 | JSObjectSetProperty(context, globalObject, myObjectBuf, myObject, kJSPropertyAttributeNone);
|
---|
557 | JSStringBufferRelease(myObjectBuf);
|
---|
558 |
|
---|
559 | JSStringBufferRef printBuf = JSStringBufferCreateUTF8("print");
|
---|
560 | JSObjectSetProperty(context, globalObject, printBuf, JSFunctionMake(context, print_callAsFunction), kJSPropertyAttributeNone);
|
---|
561 | JSStringBufferRelease(printBuf);
|
---|
562 |
|
---|
563 | JSStringBufferRef myConstructorBuf = JSStringBufferCreateUTF8("MyConstructor");
|
---|
564 | JSObjectSetProperty(context, globalObject, myConstructorBuf, JSConstructorMake(context, myConstructor_callAsConstructor), kJSPropertyAttributeNone);
|
---|
565 | JSStringBufferRelease(myConstructorBuf);
|
---|
566 |
|
---|
567 | JSClassRef nullCallbacksClass = JSClassCreate(context, NULL, NULL, NULL, NULL);
|
---|
568 | JSClassRelease(nullCallbacksClass);
|
---|
569 |
|
---|
570 | char* script = createStringWithContentsOfFile("testapi.js");
|
---|
571 | JSStringBufferRef scriptBuf = JSStringBufferCreateUTF8(script);
|
---|
572 | result = JSEvaluate(context, scriptBuf, NULL, NULL, 1, &exception);
|
---|
573 | if (JSValueIsUndefined(result))
|
---|
574 | printf("PASS: Test script executed successfully.\n");
|
---|
575 | else {
|
---|
576 | printf("FAIL: Test script returned unexcpected value:\n");
|
---|
577 | JSStringBufferRef exceptionBuf = JSValueCopyStringValue(context, exception);
|
---|
578 | CFStringRef exceptionCF = CFStringCreateWithJSStringBuffer(kCFAllocatorDefault, exceptionBuf);
|
---|
579 | CFShow(exceptionCF);
|
---|
580 | CFRelease(exceptionCF);
|
---|
581 | JSStringBufferRelease(exceptionBuf);
|
---|
582 | }
|
---|
583 | JSStringBufferRelease(scriptBuf);
|
---|
584 | free(script);
|
---|
585 |
|
---|
586 | // Allocate a few dummies so that at least one will be collected
|
---|
587 | JSObjectMake(context, MyObject_class(context), 0);
|
---|
588 | JSObjectMake(context, MyObject_class(context), 0);
|
---|
589 | JSGCCollect();
|
---|
590 | assert(didFinalize);
|
---|
591 |
|
---|
592 | JSContextDestroy(context);
|
---|
593 | printf("PASS: Program exited normally.\n");
|
---|
594 | return 0;
|
---|
595 | }
|
---|
596 |
|
---|
597 | static char* createStringWithContentsOfFile(const char* fileName)
|
---|
598 | {
|
---|
599 | char* buffer;
|
---|
600 |
|
---|
601 | int buffer_size = 0;
|
---|
602 | int buffer_capacity = 1024;
|
---|
603 | buffer = (char*)malloc(buffer_capacity);
|
---|
604 |
|
---|
605 | FILE* f = fopen(fileName, "r");
|
---|
606 | if (!f) {
|
---|
607 | fprintf(stderr, "Could not open file: %s\n", fileName);
|
---|
608 | return 0;
|
---|
609 | }
|
---|
610 |
|
---|
611 | while (!feof(f) && !ferror(f)) {
|
---|
612 | buffer_size += fread(buffer + buffer_size, 1, buffer_capacity - buffer_size, f);
|
---|
613 | if (buffer_size == buffer_capacity) { // guarantees space for trailing '\0'
|
---|
614 | buffer_capacity *= 2;
|
---|
615 | buffer = (char*)realloc(buffer, buffer_capacity);
|
---|
616 | assert(buffer);
|
---|
617 | }
|
---|
618 |
|
---|
619 | assert(buffer_size < buffer_capacity);
|
---|
620 | }
|
---|
621 | fclose(f);
|
---|
622 | buffer[buffer_size] = '\0';
|
---|
623 |
|
---|
624 | return buffer;
|
---|
625 | }
|
---|