source: webkit/trunk/JavaScriptCore/API/JSNode.c@ 15328

Last change on this file since 15328 was 15328, checked in by ggaren, 19 years ago

Reviewed by Darin.


  • Changed public header includes to the <JavaScriptCore/ style.
  • Changed instances of 'buffer' to 'string' since we decided on JSInternalString instead of JSStringBuffer.
  • API/JSContextRef.h:
  • API/JSInternalStringRef.cpp: (JSStringMake): (JSInternalStringRetain): (JSInternalStringRelease): (JSValueCopyStringValue): (JSInternalStringGetLength): (JSInternalStringGetCharactersPtr): (JSInternalStringGetCharacters): (JSInternalStringGetMaxLengthUTF8): (JSInternalStringGetCharactersUTF8): (CFStringCreateWithJSInternalString):
  • API/JSInternalStringRef.h:
  • API/JSNode.c: (JSNodePrototype_appendChild): (JSNode_getNodeType):
  • API/JSObjectRef.cpp: (JSObjectCallAsConstructor):
  • API/JSValueRef.h:
  • API/JavaScriptCore.h:
  • API/minidom.c: (main): (print):
  • API/testapi.c: (MyObject_getPropertyList): (myConstructor_callAsConstructor): (main): I noticed that we were prematurely releasing some string buffers, so I moved their release calls to the end of main(). I got rid of 'Buf' in *Buf (sometimes changing to 'IString', when necessary to differentiate a variable) to match the buffer->string change.
File size: 7.5 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 "JSNodeList.h"
29#include "Node.h"
30#include "NodeList.h"
31#include "UnusedParam.h"
32
33static JSClassRef JSNode_class(JSContextRef context);
34
35static JSValueRef JSNodePrototype_appendChild(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argc, JSValueRef argv[], JSValueRef* exception)
36{
37 UNUSED_PARAM(context);
38 UNUSED_PARAM(function);
39
40 // Example of throwing a type error for invalid values
41 if (!JSValueIsObjectOfClass(thisObject, JSNode_class(context))) {
42 JSInternalStringRef message = JSInternalStringCreateUTF8("TypeError: appendChild can only be called on nodes");
43 *exception = JSStringMake(message);
44 JSInternalStringRelease(message);
45 } else if (argc < 1 || !JSValueIsObjectOfClass(argv[0], JSNode_class(context))) {
46 JSInternalStringRef message = JSInternalStringCreateUTF8("TypeError: first argument to appendChild must be a node");
47 *exception = JSStringMake(message);
48 JSInternalStringRelease(message);
49 } else {
50 Node* node = JSObjectGetPrivate(thisObject);
51 Node* child = JSObjectGetPrivate(JSValueToObject(context, argv[0]));
52
53 Node_appendChild(node, child);
54 }
55
56 return JSUndefinedMake();
57}
58
59static JSValueRef JSNodePrototype_removeChild(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argc, JSValueRef argv[], JSValueRef* exception)
60{
61 UNUSED_PARAM(context);
62 UNUSED_PARAM(function);
63
64 // Example of ignoring invalid values
65 if (argc > 0) {
66 if (JSValueIsObjectOfClass(thisObject, JSNode_class(context))) {
67 if (JSValueIsObjectOfClass(argv[0], JSNode_class(context))) {
68 Node* node = JSObjectGetPrivate(thisObject);
69 Node* child = JSObjectGetPrivate(JSValueToObject(context, argv[0]));
70
71 Node_removeChild(node, child);
72 }
73 }
74 }
75
76 return JSUndefinedMake();
77}
78
79static JSValueRef JSNodePrototype_replaceChild(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argc, JSValueRef argv[], JSValueRef* exception)
80{
81 UNUSED_PARAM(context);
82 UNUSED_PARAM(function);
83
84 if (argc > 1) {
85 if (JSValueIsObjectOfClass(thisObject, JSNode_class(context))) {
86 if (JSValueIsObjectOfClass(argv[0], JSNode_class(context))) {
87 if (JSValueIsObjectOfClass(argv[1], JSNode_class(context))) {
88 Node* node = JSObjectGetPrivate(thisObject);
89 Node* newChild = JSObjectGetPrivate(JSValueToObject(context, argv[0]));
90 Node* oldChild = JSObjectGetPrivate(JSValueToObject(context, argv[1]));
91
92 Node_replaceChild(node, newChild, oldChild);
93 }
94 }
95 }
96 }
97
98 return JSUndefinedMake();
99}
100
101static JSStaticFunction JSNodePrototype_staticFunctions[] = {
102 { "appendChild", JSNodePrototype_appendChild, kJSPropertyAttributeDontDelete },
103 { "removeChild", JSNodePrototype_removeChild, kJSPropertyAttributeDontDelete },
104 { "replaceChild", JSNodePrototype_replaceChild, kJSPropertyAttributeDontDelete },
105 { 0, 0, 0 }
106};
107
108static JSClassRef JSNodePrototype_class(JSContextRef context)
109{
110 static JSClassRef nodePrototypeClass;
111 if (!nodePrototypeClass)
112 nodePrototypeClass = JSClassCreate(NULL, JSNodePrototype_staticFunctions, &kJSObjectCallbacksNone, NULL);
113 return nodePrototypeClass;
114}
115
116static bool JSNode_getNodeType(JSContextRef context, JSObjectRef object, JSInternalStringRef propertyName, JSValueRef* returnValue, JSValueRef* exception)
117{
118 UNUSED_PARAM(context);
119 UNUSED_PARAM(propertyName);
120
121 Node* node = JSObjectGetPrivate(object);
122 if (node) {
123 JSInternalStringRef nodeType = JSInternalStringCreateUTF8(node->nodeType);
124 *returnValue = JSStringMake(nodeType);
125 JSInternalStringRelease(nodeType);
126 return true;
127 }
128 return false;
129}
130
131static bool JSNode_getChildNodes(JSContextRef context, JSObjectRef thisObject, JSInternalStringRef propertyName, JSValueRef* returnValue, JSValueRef* exception)
132{
133 UNUSED_PARAM(propertyName);
134 Node* node = JSObjectGetPrivate(thisObject);
135 assert(node);
136 *returnValue = JSNodeList_new(context, NodeList_new(node));
137 return true;
138}
139
140static bool JSNode_getFirstChild(JSContextRef context, JSObjectRef object, JSInternalStringRef propertyName, JSValueRef* returnValue, JSValueRef* exception)
141{
142 UNUSED_PARAM(context);
143 UNUSED_PARAM(propertyName);
144 UNUSED_PARAM(object);
145
146 *returnValue = JSUndefinedMake();
147 return true;
148}
149
150static JSStaticValue JSNode_staticValues[] = {
151 { "nodeType", JSNode_getNodeType, NULL, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly },
152 { "childNodes", JSNode_getChildNodes, NULL, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly },
153 { "firstChild", JSNode_getFirstChild, NULL, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly },
154 { 0, 0, 0, 0 }
155};
156
157static void JSNode_finalize(JSObjectRef object)
158{
159 Node* node = JSObjectGetPrivate(object);
160 assert(node);
161
162 Node_deref(node);
163}
164
165static JSClassRef JSNode_class(JSContextRef context)
166{
167 static JSClassRef nodeClass;
168 if (!nodeClass) {
169 JSObjectCallbacks JSNode_callbacks = kJSObjectCallbacksNone;
170 JSNode_callbacks.finalize = JSNode_finalize;
171
172 nodeClass = JSClassCreate(JSNode_staticValues, NULL, &JSNode_callbacks, NULL);
173 }
174 return nodeClass;
175}
176
177static JSObjectRef JSNode_prototype(JSContextRef context)
178{
179 static JSObjectRef prototype;
180 if (!prototype) {
181 prototype = JSObjectMake(context, JSNodePrototype_class(context), NULL);
182 JSGCProtect(prototype);
183 }
184 return prototype;
185}
186
187JSObjectRef JSNode_new(JSContextRef context, Node* node)
188{
189 Node_ref(node);
190
191 JSObjectRef jsNode = JSObjectMake(context, JSNode_class(context), JSNode_prototype(context));
192 JSObjectSetPrivate(jsNode, node);
193 return jsNode;
194}
195
196JSObjectRef JSNode_construct(JSContextRef context, JSObjectRef object, size_t argc, JSValueRef argv[], JSValueRef* exception)
197{
198 UNUSED_PARAM(object);
199 UNUSED_PARAM(argc);
200 UNUSED_PARAM(argv);
201
202 return JSNode_new(context, Node_new());
203}
Note: See TracBrowser for help on using the repository browser.