source: webkit/trunk/JavaScriptCore/API/minidom.c@ 21342

Last change on this file since 21342 was 21342, checked in by ggaren, 18 years ago

Reviewed by Darin Adler.


Fixed #includes of JSStringRefCF.h and use of CF datatypes. I think I
misunderstood this issue before.

  • API/JavaScriptCore.h: #include JSStringRefCF.h. Platforms that don't want this behavior can just #include individual headers, instead of the umbrella framework header. But we definitely want Mac OS X clients to get the #include of JSStringRefCF.h "for free."
  • API/minidom.c: Don't #include JSStringRefCF.h. (Don't need to #include JavaScriptCore.h, either.)
  • API/testapi.c: Don't #include JSStringRefCF.h. Do use CF datatypes regardless of whether APPLE is defined. Platforms that don't support CF just shouldn't compile this file. (main):
File size: 4.6 KB
Line 
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 "JSNode.h"
28#include <wtf/UnusedParam.h>
29
30static char* createStringWithContentsOfFile(const char* fileName);
31static JSValueRef print(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
32
33int main(int argc, char* argv[])
34{
35 UNUSED_PARAM(argc);
36 UNUSED_PARAM(argv);
37
38 JSGlobalContextRef context = JSGlobalContextCreate(NULL);
39 JSObjectRef globalObject = JSContextGetGlobalObject(context);
40
41 JSStringRef printIString = JSStringCreateWithUTF8CString("print");
42 JSObjectSetProperty(context, globalObject, printIString, JSObjectMakeFunctionWithCallback(context, printIString, print), kJSPropertyAttributeNone, NULL);
43 JSStringRelease(printIString);
44
45 JSStringRef node = JSStringCreateWithUTF8CString("Node");
46 JSObjectSetProperty(context, globalObject, node, JSObjectMakeConstructor(context, JSNode_class(context), JSNode_construct), kJSPropertyAttributeNone, NULL);
47 JSStringRelease(node);
48
49 char* scriptUTF8 = createStringWithContentsOfFile("minidom.js");
50 JSStringRef script = JSStringCreateWithUTF8CString(scriptUTF8);
51 JSValueRef exception;
52 JSValueRef result = JSEvaluateScript(context, script, NULL, NULL, 0, &exception);
53 if (result)
54 printf("PASS: Test script executed successfully.\n");
55 else {
56 printf("FAIL: Test script threw exception:\n");
57 JSStringRef exceptionIString = JSValueToStringCopy(context, exception, NULL);
58 CFStringRef exceptionCF = JSStringCopyCFString(kCFAllocatorDefault, exceptionIString);
59 CFShow(exceptionCF);
60 CFRelease(exceptionCF);
61 JSStringRelease(exceptionIString);
62 }
63 JSStringRelease(script);
64 free(scriptUTF8);
65
66 globalObject = 0;
67 JSGlobalContextRelease(context);
68 JSGarbageCollect(context);
69 printf("PASS: Program exited normally.\n");
70 return 0;
71}
72
73static JSValueRef print(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
74{
75 if (argumentCount > 0) {
76 JSStringRef string = JSValueToStringCopy(context, arguments[0], NULL);
77 size_t numChars = JSStringGetMaximumUTF8CStringSize(string);
78 char stringUTF8[numChars];
79 JSStringGetUTF8CString(string, stringUTF8, numChars);
80 printf("%s\n", stringUTF8);
81 }
82
83 return JSValueMakeUndefined(context);
84}
85
86static char* createStringWithContentsOfFile(const char* fileName)
87{
88 char* buffer;
89
90 size_t buffer_size = 0;
91 size_t buffer_capacity = 1024;
92 buffer = (char*)malloc(buffer_capacity);
93
94 FILE* f = fopen(fileName, "r");
95 if (!f) {
96 fprintf(stderr, "Could not open file: %s\n", fileName);
97 return 0;
98 }
99
100 while (!feof(f) && !ferror(f)) {
101 buffer_size += fread(buffer + buffer_size, 1, buffer_capacity - buffer_size, f);
102 if (buffer_size == buffer_capacity) { // guarantees space for trailing '\0'
103 buffer_capacity *= 2;
104 buffer = (char*)realloc(buffer, buffer_capacity);
105 assert(buffer);
106 }
107
108 assert(buffer_size < buffer_capacity);
109 }
110 fclose(f);
111 buffer[buffer_size] = '\0';
112
113 return buffer;
114}
Note: See TracBrowser for help on using the repository browser.