Changeset 15133 in webkit for trunk/JavaScriptCore/API
- Timestamp:
- Jul 1, 2006, 9:06:07 PM (19 years ago)
- Location:
- trunk/JavaScriptCore/API
- Files:
-
- 14 added
- 14 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/API/JSBase.h
r15096 r15133 31 31 typedef struct __JSContext* JSContextRef; 32 32 typedef struct __JSCharBuffer* JSCharBufferRef; 33 typedef struct __JSClass* JSClassRef; 33 34 typedef struct __JSPropertyList* JSPropertyListRef; 34 typedef struct __JSProperty ListEnumerator* JSPropertyListEnumeratorRef;35 typedef struct __JSPropertyEnumerator* JSPropertyEnumeratorRef; 35 36 36 37 /* Base type of all JS values, and polymorphic functions on them */ -
trunk/JavaScriptCore/API/JSCallbackObject.cpp
r14951 r15133 28 28 #include "JSCallbackObject.h" 29 29 #include "JSCharBufferRef.h" 30 #include "JSClassRef.h" 30 31 #include "JSObjectRef.h" 31 32 #include "internal.h" 33 #include "reference.h" 32 34 #include "reference_list.h" 33 35 34 36 namespace KJS { 35 37 36 const ClassInfo JSCallbackObject::info = { " JSCallbackObject", 0, 0, 0 };37 38 JSCallbackObject::JSCallbackObject( const JSObjectCallbacks* callbacks)38 const ClassInfo JSCallbackObject::info = { "CallbackObject", 0, 0, 0 }; 39 40 JSCallbackObject::JSCallbackObject(JSClassRef jsClass) 39 41 : JSObject() 40 , m_privateData(0) 41 , m_callbacks(*callbacks) 42 { 42 { 43 init(jsClass); 44 } 45 46 JSCallbackObject::JSCallbackObject(JSClassRef jsClass, JSObject* prototype) 47 : JSObject(prototype) 48 { 49 init(jsClass); 50 } 51 52 void JSCallbackObject::init(JSClassRef jsClass) 53 { 54 m_privateData = 0; 55 m_class = JSClassRetain(jsClass); 56 43 57 JSObjectRef thisRef = toRef(this); 44 58 45 59 do { 46 if (JSInitializeCallback initialize = callbacks->initialize)60 if (JSInitializeCallback initialize = jsClass->callbacks.initialize) 47 61 initialize(thisRef); 48 } while ((callbacks = callbacks->parentCallbacks)); 49 } 50 51 JSCallbackObject::JSCallbackObject(const JSObjectCallbacks* callbacks, JSObject* prototype) 52 : JSObject(prototype) 53 , m_privateData(0) 54 , m_callbacks(*callbacks) 55 { 56 JSObjectRef thisRef = toRef(this); 57 58 do { 59 if (JSInitializeCallback initialize = callbacks->initialize) 60 initialize(thisRef); 61 } while ((callbacks = callbacks->parentCallbacks)); 62 } while ((jsClass = jsClass->parent)); 62 63 } 63 64 … … 66 67 JSObjectRef thisRef = toRef(this); 67 68 68 for (JS ObjectCallbacks* callbacks = &m_callbacks; callbacks; callbacks = callbacks->parentCallbacks)69 if (JSFinalizeCallback finalize = callbacks->finalize)69 for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parent) 70 if (JSFinalizeCallback finalize = jsClass->callbacks.finalize) 70 71 finalize(thisRef); 72 73 JSClassRelease(m_class); 71 74 } 72 75 73 76 UString JSCallbackObject::className() const 74 77 { 75 JSObjectRef thisRef = toRef(this); 76 77 for (const JSObjectCallbacks* callbacks = &m_callbacks; callbacks; callbacks = callbacks->parentCallbacks) { 78 if (JSCopyDescriptionCallback copyDescriptionCallback = callbacks->copyDescription) { 79 JSCharBufferRef descriptionBuf = copyDescriptionCallback(thisRef); 80 UString description(toJS(descriptionBuf)); 81 JSCharBufferRelease(descriptionBuf); 82 return description; 83 } 84 } 85 86 return JSObject::className(); 78 return classInfo()->className; 87 79 } 88 80 … … 91 83 JSObjectRef thisRef = toRef(this); 92 84 JSCharBufferRef propertyNameRef = toRef(propertyName.ustring().rep()); 93 94 // optional optimization for cases when we only need to know if the property exists, not its value95 for (const JSObjectCallbacks* callbacks = &m_callbacks; callbacks; callbacks = callbacks->parentCallbacks) {96 if (JSHasPropertyCallback hasPropertyCallback = callbacks->hasProperty) {85 86 for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parent) { 87 // optional optimization to bypass getProperty in cases when we only need to know if the property exists 88 if (JSHasPropertyCallback hasPropertyCallback = jsClass->callbacks.hasProperty) { 97 89 if (hasPropertyCallback(thisRef, propertyNameRef)) { 98 slot.setCustom(0, callbackGetter); 99 return true; 100 } 101 } 102 } 103 104 for (const JSObjectCallbacks* callbacks = &m_callbacks; callbacks; callbacks = callbacks->parentCallbacks) { 105 if (JSGetPropertyCallback getPropertyCallback = callbacks->getProperty) { 90 slot.setCustom(this, callbackGetter); 91 return true; 92 } 93 } else if (JSGetPropertyCallback getPropertyCallback = jsClass->callbacks.getProperty) { 106 94 JSValueRef returnValue; 107 if (getPropertyCallback(thisRef, propertyNameRef, &returnValue)) { 108 slot.setCustom(reinterpret_cast<JSObject*>(returnValue), cachedValueGetter); // cache the value so we don't have to compute it again 109 return true; 110 } 111 } 112 } 95 if (getPropertyCallback(toRef(exec), thisRef, propertyNameRef, &returnValue)) { 96 // cache the value so we don't have to compute it again 97 // FIXME: This violates the PropertySlot design a little bit. 98 // We should either use this optimization everywhere, or nowhere. 99 slot.setCustom(reinterpret_cast<JSObject*>(returnValue), cachedValueGetter); 100 return true; 101 } 102 } 103 104 if (__JSClass::StaticValuesTable* staticValues = jsClass->staticValues) { 105 if (StaticValueEntry* entry = staticValues->get(propertyName.ustring().rep())) { 106 if (entry->getProperty) { 107 slot.setCustom(this, staticValueGetter); 108 return true; 109 } 110 } 111 } 112 113 if (__JSClass::StaticFunctionsTable* staticFunctions = jsClass->staticFunctions) { 114 if (staticFunctions->contains(propertyName.ustring().rep())) { 115 slot.setCustom(this, staticFunctionGetter); 116 return true; 117 } 118 } 119 } 120 113 121 return JSObject::getOwnPropertySlot(exec, propertyName, slot); 114 122 } … … 124 132 JSCharBufferRef propertyNameRef = toRef(propertyName.ustring().rep()); 125 133 126 for ( const JSObjectCallbacks* callbacks = &m_callbacks; callbacks; callbacks = callbacks->parentCallbacks) {127 if (JSSetPropertyCallback setPropertyCallback = callbacks->setProperty) {134 for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parent) { 135 if (JSSetPropertyCallback setPropertyCallback = jsClass->callbacks.setProperty) { 128 136 if (setPropertyCallback(thisRef, propertyNameRef, value)) 129 137 return; 130 138 } 139 140 if (__JSClass::StaticValuesTable* staticValues = jsClass->staticValues) { 141 if (StaticValueEntry* entry = staticValues->get(propertyName.ustring().rep())) { 142 if (entry->attributes & kJSPropertyAttributeReadOnly) 143 return; 144 if (JSSetPropertyCallback setPropertyCallback = entry->setProperty) { 145 if (setPropertyCallback(thisRef, propertyNameRef, value)) 146 return; 147 } 148 } 149 } 150 151 if (__JSClass::StaticFunctionsTable* staticFunctions = jsClass->staticFunctions) { 152 if (StaticFunctionEntry* entry = staticFunctions->get(propertyName.ustring().rep())) { 153 if (entry->attributes & kJSPropertyAttributeReadOnly) 154 return; 155 putDirect(propertyName, value, attr); // put as override property 156 return; 157 } 158 } 131 159 } 132 160 return JSObject::put(exec, propertyName, value, attr); … … 143 171 JSCharBufferRef propertyNameRef = toRef(propertyName.ustring().rep()); 144 172 145 for ( const JSObjectCallbacks* callbacks = &m_callbacks; callbacks; callbacks = callbacks->parentCallbacks) {146 if (JSDeletePropertyCallback deletePropertyCallback = callbacks->deleteProperty) {173 for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parent) { 174 if (JSDeletePropertyCallback deletePropertyCallback = jsClass->callbacks.deleteProperty) { 147 175 if (deletePropertyCallback(thisRef, propertyNameRef)) 148 176 return true; 149 177 } 178 179 if (__JSClass::StaticValuesTable* staticValues = jsClass->staticValues) { 180 if (StaticValueEntry* entry = staticValues->get(propertyName.ustring().rep())) { 181 if (entry->attributes & kJSPropertyAttributeDontDelete) 182 return false; 183 return true; 184 } 185 } 186 187 if (__JSClass::StaticFunctionsTable* staticFunctions = jsClass->staticFunctions) { 188 if (StaticFunctionEntry* entry = staticFunctions->get(propertyName.ustring().rep())) { 189 if (entry->attributes & kJSPropertyAttributeDontDelete) 190 return false; 191 return true; 192 } 193 } 150 194 } 151 195 return JSObject::deleteProperty(exec, propertyName); … … 159 203 bool JSCallbackObject::implementsConstruct() const 160 204 { 161 for ( const JSObjectCallbacks* callbacks = &m_callbacks; callbacks; callbacks = callbacks->parentCallbacks)162 if ( callbacks->callAsConstructor)205 for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parent) 206 if (jsClass->callbacks.callAsConstructor) 163 207 return true; 164 208 … … 171 215 JSObjectRef thisRef = toRef(this); 172 216 173 for ( const JSObjectCallbacks* callbacks = &m_callbacks; callbacks; callbacks = callbacks->parentCallbacks) {174 if (JSCallAsConstructorCallback callAsConstructorCallback = callbacks->callAsConstructor) {217 for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parent) { 218 if (JSCallAsConstructorCallback callAsConstructorCallback = jsClass->callbacks.callAsConstructor) { 175 219 size_t argc = args.size(); 176 220 JSValueRef argv[argc]; … … 181 225 } 182 226 183 ASSERT( false);227 ASSERT(0); // implementsConstruct should prevent us from reaching here 184 228 return 0; 185 229 } … … 187 231 bool JSCallbackObject::implementsCall() const 188 232 { 189 for ( const JSObjectCallbacks* callbacks = &m_callbacks; callbacks; callbacks = callbacks->parentCallbacks)190 if ( callbacks->callAsFunction)233 for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parent) 234 if (jsClass->callbacks.callAsFunction) 191 235 return true; 192 236 … … 200 244 JSObjectRef thisObjRef = toRef(thisObj); 201 245 202 for ( const JSObjectCallbacks* callbacks = &m_callbacks; callbacks; callbacks = callbacks->parentCallbacks) {203 if (JSCallAsFunctionCallback callAsFunctionCallback = callbacks->callAsFunction) {246 for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parent) { 247 if (JSCallAsFunctionCallback callAsFunctionCallback = jsClass->callbacks.callAsFunction) { 204 248 size_t argc = args.size(); 205 249 JSValueRef argv[argc]; … … 210 254 } 211 255 212 ASSERT( false);256 ASSERT(0); // implementsCall should prevent us from reaching here 213 257 return 0; 214 258 } … … 218 262 JSObjectRef thisRef = toRef(this); 219 263 220 for ( const JSObjectCallbacks* callbacks = &m_callbacks; callbacks; callbacks = callbacks->parentCallbacks)221 if (JSGetPropertyListCallback getPropertyListCallback = callbacks->getPropertyList)264 for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parent) { 265 if (JSGetPropertyListCallback getPropertyListCallback = jsClass->callbacks.getPropertyList) 222 266 getPropertyListCallback(thisRef, toRef(&propertyList)); 223 267 268 if (__JSClass::StaticValuesTable* staticValues = jsClass->staticValues) { 269 typedef __JSClass::StaticValuesTable::const_iterator iterator; 270 iterator end = staticValues->end(); 271 for (iterator it = staticValues->begin(); it != end; ++it) { 272 UString::Rep* name = it->first.get(); 273 StaticValueEntry* entry = it->second; 274 if (entry->getProperty && !(entry->attributes & kJSPropertyAttributeDontEnum)) 275 propertyList.append(Reference(this, Identifier(name))); 276 } 277 } 278 279 if (__JSClass::StaticFunctionsTable* staticFunctions = jsClass->staticFunctions) { 280 typedef __JSClass::StaticFunctionsTable::const_iterator iterator; 281 iterator end = staticFunctions->end(); 282 for (iterator it = staticFunctions->begin(); it != end; ++it) { 283 UString::Rep* name = it->first.get(); 284 StaticFunctionEntry* entry = it->second; 285 if (!(entry->attributes & kJSPropertyAttributeDontEnum)) 286 propertyList.append(Reference(this, Identifier(name))); 287 } 288 } 289 } 290 224 291 JSObject::getPropertyList(exec, propertyList, recursive); 225 292 } … … 229 296 JSObjectRef thisRef = toRef(this); 230 297 231 for ( const JSObjectCallbacks* callbacks = &m_callbacks; callbacks; callbacks = callbacks->parentCallbacks) {232 if (JSConvertToTypeCallback convertToTypeCallback = callbacks->convertToType) {298 for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parent) { 299 if (JSConvertToTypeCallback convertToTypeCallback = jsClass->callbacks.convertToType) { 233 300 JSValueRef returnValue; 234 301 if (convertToTypeCallback(thisRef, kJSTypeBoolean, &returnValue)) … … 243 310 JSObjectRef thisRef = toRef(this); 244 311 245 for ( const JSObjectCallbacks* callbacks = &m_callbacks; callbacks; callbacks = callbacks->parentCallbacks) {246 if (JSConvertToTypeCallback convertToTypeCallback = callbacks->convertToType) {312 for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parent) { 313 if (JSConvertToTypeCallback convertToTypeCallback = jsClass->callbacks.convertToType) { 247 314 JSValueRef returnValue; 248 315 if (convertToTypeCallback(thisRef, kJSTypeNumber, &returnValue)) … … 257 324 JSObjectRef thisRef = toRef(this); 258 325 259 for ( const JSObjectCallbacks* callbacks = &m_callbacks; callbacks; callbacks = callbacks->parentCallbacks) {260 if (JSConvertToTypeCallback convertToTypeCallback = callbacks->convertToType) {326 for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parent) { 327 if (JSConvertToTypeCallback convertToTypeCallback = jsClass->callbacks.convertToType) { 261 328 JSValueRef returnValue; 262 329 if (convertToTypeCallback(thisRef, kJSTypeString, &returnValue)) … … 277 344 } 278 345 346 bool JSCallbackObject::inherits(JSClassRef c) const 347 { 348 for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parent) 349 if (jsClass == c) 350 return true; 351 return false; 352 } 353 279 354 JSValue* JSCallbackObject::cachedValueGetter(ExecState*, JSObject*, const Identifier&, const PropertySlot& slot) 280 355 { … … 284 359 } 285 360 286 JSValue* JSCallbackObject::callbackGetter(ExecState*, JSObject* originalObject, const Identifier& propertyName, const PropertySlot&) 287 { 288 ASSERT(originalObject->inherits(&JSCallbackObject::info)); 289 JSObjectRef thisRef = toRef(originalObject); 290 JSCharBufferRef propertyNameRef= toRef(propertyName.ustring().rep()); 291 292 for (const JSObjectCallbacks* callbacks = &static_cast<JSCallbackObject*>(originalObject)->m_callbacks; callbacks; callbacks = callbacks->parentCallbacks) { 293 if (JSGetPropertyCallback getPropertyCallback = callbacks->getProperty) { 294 JSValueRef returnValue; 295 if (getPropertyCallback(thisRef, propertyNameRef, &returnValue)) { 361 JSValue* JSCallbackObject::staticValueGetter(ExecState* exec, JSObject*, const Identifier& propertyName, const PropertySlot& slot) 362 { 363 ASSERT(slot.slotBase()->inherits(&JSCallbackObject::info)); 364 JSCallbackObject* thisObj = static_cast<JSCallbackObject*>(slot.slotBase()); 365 366 JSObjectRef thisRef = toRef(thisObj); 367 JSCharBufferRef propertyNameRef = toRef(propertyName.ustring().rep()); 368 369 for (JSClassRef jsClass = thisObj->m_class; jsClass; jsClass = jsClass->parent) { 370 JSValueRef returnValue; 371 372 if (__JSClass::StaticValuesTable* staticValues = jsClass->staticValues) 373 if (StaticValueEntry* entry = staticValues->get(propertyName.ustring().rep())) 374 if (JSGetPropertyCallback getPropertyCallback = entry->getProperty) 375 if (getPropertyCallback(toRef(exec), thisRef, propertyNameRef, &returnValue)) 376 return toJS(returnValue); 377 } 378 379 return jsUndefined(); 380 } 381 382 JSValue* JSCallbackObject::staticFunctionGetter(ExecState* exec, JSObject*, const Identifier& propertyName, const PropertySlot& slot) 383 { 384 ASSERT(slot.slotBase()->inherits(&JSCallbackObject::info)); 385 JSCallbackObject* thisObj = static_cast<JSCallbackObject*>(slot.slotBase()); 386 387 if (JSValue* cachedOrOverrideValue = thisObj->getDirect(propertyName)) 388 return toJS(cachedOrOverrideValue); 389 390 for (JSClassRef jsClass = thisObj->m_class; jsClass; jsClass = jsClass->parent) { 391 if (__JSClass::StaticFunctionsTable* staticFunctions = jsClass->staticFunctions) { 392 if (StaticFunctionEntry* entry = staticFunctions->get(propertyName.ustring().rep())) { 393 JSValue* v = toJS(JSFunctionMake(toRef(exec), entry->callAsFunction)); 394 thisObj->putDirect(propertyName, v, entry->attributes); 395 return v; 396 } 397 } 398 } 399 400 return jsUndefined(); 401 } 402 403 JSValue* JSCallbackObject::callbackGetter(ExecState* exec, JSObject*, const Identifier& propertyName, const PropertySlot& slot) 404 { 405 ASSERT(slot.slotBase()->inherits(&JSCallbackObject::info)); 406 JSCallbackObject* thisObj = static_cast<JSCallbackObject*>(slot.slotBase()); 407 408 JSObjectRef thisRef = toRef(thisObj); 409 JSCharBufferRef propertyNameRef = toRef(propertyName.ustring().rep()); 410 411 for (JSClassRef jsClass = thisObj->m_class; jsClass; jsClass = jsClass->parent) { 412 JSValueRef returnValue; 413 414 if (JSGetPropertyCallback getPropertyCallback = jsClass->callbacks.getProperty) 415 if (getPropertyCallback(toRef(exec), thisRef, propertyNameRef, &returnValue)) 296 416 return toJS(returnValue); 297 }298 }299 417 } 300 418 -
trunk/JavaScriptCore/API/JSCallbackObject.h
r14951 r15133 37 37 { 38 38 public: 39 JSCallbackObject( const JSObjectCallbacks* callbacks);40 JSCallbackObject( const JSObjectCallbacks* callbacks, JSObject* prototype);39 JSCallbackObject(JSClassRef globalObjectClass); 40 JSCallbackObject(JSClassRef globalObjectClass, JSObject* prototype); 41 41 virtual ~JSCallbackObject(); 42 42 … … 69 69 virtual const ClassInfo *classInfo() const { return &info; } 70 70 static const ClassInfo info; 71 72 bool inherits(JSClassRef) const; 71 73 72 74 private: … … 74 76 JSCallbackObject(const JSCallbackObject&); 75 77 78 void init(JSClassRef jsClass); 79 76 80 static JSValue* cachedValueGetter(ExecState*, JSObject*, const Identifier&, const PropertySlot&); 81 static JSValue* staticValueGetter(ExecState*, JSObject*, const Identifier&, const PropertySlot& slot); 82 static JSValue* staticFunctionGetter(ExecState*, JSObject*, const Identifier&, const PropertySlot& slot); 77 83 static JSValue* callbackGetter(ExecState*, JSObject*, const Identifier&, const PropertySlot&); 84 78 85 void* m_privateData; 79 JS ObjectCallbacks m_callbacks;86 JSClassRef m_class; 80 87 }; 81 88 -
trunk/JavaScriptCore/API/JSCharBufferRef.cpp
r14951 r15133 28 28 #include "JSCharBufferRef.h" 29 29 30 #include < JavaScriptCore/JSLock.h>31 #include < JavaScriptCore/JSType.h>32 #include < JavaScriptCore/internal.h>33 #include < JavaScriptCore/operations.h>34 #include < JavaScriptCore/ustring.h>35 #include < JavaScriptCore/value.h>30 #include <kjs/JSLock.h> 31 #include <kjs/JSType.h> 32 #include <kjs/internal.h> 33 #include <kjs/operations.h> 34 #include <kjs/ustring.h> 35 #include <kjs/value.h> 36 36 37 37 using namespace KJS; -
trunk/JavaScriptCore/API/JSContextRef.cpp
r14951 r15133 35 35 using namespace KJS; 36 36 37 JSContextRef JSContextCreate( const JSObjectCallbacks* globalObjectCallbacks, JSObjectRef globalObjectPrototype)37 JSContextRef JSContextCreate(JSClassRef globalObjectClass, JSObjectRef globalObjectPrototype) 38 38 { 39 39 JSLock lock; … … 42 42 43 43 JSObject* globalObject; 44 if (globalObjectCallbacks == &kJSObjectCallbacksNone) // slightly more efficient 44 if (globalObjectClass) { 45 if (jsPrototype) 46 globalObject = new JSCallbackObject(globalObjectClass, jsPrototype); 47 else 48 globalObject = new JSCallbackObject(globalObjectClass); 49 } else { 50 // creates a slightly more efficient object 45 51 if (jsPrototype) 46 52 globalObject = new JSObject(jsPrototype); 47 53 else 48 54 globalObject = new JSObject(); 49 else if (jsPrototype) 50 globalObject = new JSCallbackObject(globalObjectCallbacks, jsPrototype); 51 else 52 globalObject = new JSCallbackObject(globalObjectCallbacks); 55 } 53 56 54 57 Interpreter* interpreter = new Interpreter(globalObject); // adds the built-in object prototype to the global object … … 78 81 Completion completion = exec->dynamicInterpreter()->evaluate(UString(sourceURLRep), startingLineNumber, UString(scriptRep), jsThisValue); 79 82 80 *returnValue = toRef(completion.value()); 83 if (returnValue) 84 *returnValue = completion.value() ? toRef(completion.value()) : toRef(jsUndefined()); 85 81 86 return completion.complType() != Throw; 82 87 } -
trunk/JavaScriptCore/API/JSContextRef.h
r14951 r15133 35 35 #endif 36 36 37 JSContextRef JSContextCreate( const JSObjectCallbacks* globalObjectCallbacks, JSObjectRef globalObjectPrototype);37 JSContextRef JSContextCreate(JSClassRef globalObjectClass, JSObjectRef globalObjectPrototype); 38 38 void JSContextDestroy(JSContextRef context); 39 39 -
trunk/JavaScriptCore/API/JSObjectRef.cpp
r15043 r15133 28 28 #include "JSValueRef.h" 29 29 #include "JSObjectRef.h" 30 #include "JSCallbackConstructor.h" 31 #include "JSCallbackFunction.h" 30 32 #include "JSCallbackObject.h" 31 33 … … 36 38 37 39 using namespace KJS; 38 39 const JSObjectCallbacks kJSObjectCallbacksNone = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };40 40 41 41 JSObjectRef JSValueToObject(JSContextRef context, JSValueRef value) … … 52 52 } 53 53 54 JSObjectRef JSObjectMake(JSContextRef context, const JSObjectCallbacks* callbacks, JSObjectRef prototype)54 JSObjectRef JSObjectMake(JSContextRef context, JSClassRef jsClass, JSObjectRef prototype) 55 55 { 56 56 JSLock lock; … … 62 62 jsPrototype = exec->lexicalInterpreter()->builtinObjectPrototype(); 63 63 64 if ( callbacks == &kJSObjectCallbacksNone)64 if (!jsClass) 65 65 return toRef(new JSObject(jsPrototype)); // slightly more efficient 66 66 else 67 return toRef(new JSCallbackObject( callbacks, jsPrototype));67 return toRef(new JSCallbackObject(jsClass, jsPrototype)); 68 68 } 69 69 70 70 JSObjectRef JSFunctionMake(JSContextRef context, JSCallAsFunctionCallback callback) 71 71 { 72 ExecState* exec = toJS(context); 73 JSObjectCallbacks callbacks = kJSObjectCallbacksNone; 74 callbacks.callAsFunction = callback; 75 76 return JSObjectMake(context, &callbacks, toRef(exec->lexicalInterpreter()->builtinFunctionPrototype())); 72 JSLock lock; 73 ExecState* exec = toJS(context); 74 return toRef(new JSCallbackFunction(exec, callback)); 77 75 } 78 76 79 77 JSObjectRef JSConstructorMake(JSContextRef context, JSCallAsConstructorCallback callback) 80 78 { 81 ExecState* exec = toJS(context); 82 JSObjectCallbacks callbacks = kJSObjectCallbacksNone; 83 callbacks.callAsConstructor = callback; 84 85 return JSObjectMake(context, &callbacks, toRef(exec->lexicalInterpreter()->builtinObjectPrototype())); 79 JSLock lock; 80 ExecState* exec = toJS(context); 81 return toRef(new JSCallbackConstructor(exec, callback)); 86 82 } 87 83 … … 224 220 } 225 221 226 struct __JSProperty ListEnumerator227 { 228 __JSProperty ListEnumerator() : refCount(0), iterator(list.end())222 struct __JSPropertyEnumerator 223 { 224 __JSPropertyEnumerator() : refCount(0), iterator(list.end()) 229 225 { 230 226 } … … 235 231 }; 236 232 237 JSProperty ListEnumeratorRef JSObjectCreatePropertyEnumerator(JSContextRef context, JSObjectRef object)238 { 239 JSLock lock; 240 ExecState* exec = toJS(context); 241 JSObject* jsObject = toJS(object); 242 243 JSProperty ListEnumeratorRef enumerator = new __JSPropertyListEnumerator();233 JSPropertyEnumeratorRef JSObjectCreatePropertyEnumerator(JSContextRef context, JSObjectRef object) 234 { 235 JSLock lock; 236 ExecState* exec = toJS(context); 237 JSObject* jsObject = toJS(object); 238 239 JSPropertyEnumeratorRef enumerator = new __JSPropertyEnumerator(); 244 240 jsObject->getPropertyList(exec, enumerator->list); 245 241 enumerator->iterator = enumerator->list.begin(); 246 242 247 return enumerator;248 } 249 250 JSCharBufferRef JSPropertyEnumeratorGetNext(JSContextRef context, JSProperty ListEnumeratorRef enumerator)243 return JSPropertyEnumeratorRetain(enumerator); 244 } 245 246 JSCharBufferRef JSPropertyEnumeratorGetNext(JSContextRef context, JSPropertyEnumeratorRef enumerator) 251 247 { 252 248 ExecState* exec = toJS(context); … … 259 255 } 260 256 261 JSProperty ListEnumeratorRef JSPropertyEnumeratorRetain(JSPropertyListEnumeratorRef enumerator)257 JSPropertyEnumeratorRef JSPropertyEnumeratorRetain(JSPropertyEnumeratorRef enumerator) 262 258 { 263 259 ++enumerator->refCount; … … 265 261 } 266 262 267 void JSPropertyEnumeratorRelease(JSProperty ListEnumeratorRef enumerator)263 void JSPropertyEnumeratorRelease(JSPropertyEnumeratorRef enumerator) 268 264 { 269 265 if (--enumerator->refCount == 0) -
trunk/JavaScriptCore/API/JSObjectRef.h
r15043 r15133 29 29 30 30 #include "JSBase.h" 31 #include "JSValueRef.h" 31 32 32 33 #ifdef __cplusplus … … 48 49 (*JSFinalizeCallback) (JSObjectRef object); 49 50 50 typedef JSCharBufferRef51 (*JSCopyDescriptionCallback) (JSObjectRef object);52 53 51 typedef bool 54 52 (*JSHasPropertyCallback) (JSObjectRef object, JSCharBufferRef propertyName); 55 53 56 54 typedef bool 57 (*JSGetPropertyCallback) (JS ObjectRef object, JSCharBufferRef propertyName, JSValueRef* returnValue);55 (*JSGetPropertyCallback) (JSContextRef context, JSObjectRef object, JSCharBufferRef propertyName, JSValueRef* returnValue); 58 56 59 57 typedef bool … … 77 75 typedef struct __JSObjectCallbacks { 78 76 int version; // current (and only) version is 0 79 struct __JSObjectCallbacks* parentCallbacks; // pass NULL for the default object callbacks80 77 JSInitializeCallback initialize; 81 78 JSFinalizeCallback finalize; 82 JSCopyDescriptionCallback copyDescription;83 79 JSHasPropertyCallback hasProperty; 84 80 JSGetPropertyCallback getProperty; … … 93 89 extern const JSObjectCallbacks kJSObjectCallbacksNone; 94 90 91 typedef struct { 92 const char* const name; // FIXME: convert UTF8 93 JSGetPropertyCallback getProperty; 94 JSSetPropertyCallback setProperty; 95 JSPropertyAttributes attributes; 96 } JSStaticValue; 97 98 typedef struct { 99 const char* const name; // FIXME: convert UTF8 100 JSCallAsFunctionCallback callAsFunction; 101 JSPropertyAttributes attributes; 102 } JSStaticFunction; 103 104 JSClassRef JSClassCreate(JSContextRef context, JSStaticValue* staticValues, JSStaticFunction* staticFunctions, const JSObjectCallbacks* callbacks, JSClassRef parentClass); 105 void JSClassRelease(JSClassRef jsClass); 106 JSClassRef JSClassRetain(JSClassRef jsClass); 107 95 108 // pass NULL as prototype to get the built-in object prototype 96 JSObjectRef JSObjectMake(JSContextRef context, const JSObjectCallbacks* callbacks, JSObjectRef prototype);109 JSObjectRef JSObjectMake(JSContextRef context, JSClassRef jsClass, JSObjectRef prototype); 97 110 98 111 // Will be assigned the built-in function prototype … … 120 133 121 134 // Used for enumerating the names of an object's properties like a for...in loop would 122 JSProperty ListEnumeratorRef JSObjectCreatePropertyEnumerator(JSContextRef context, JSObjectRef object);123 JSProperty ListEnumeratorRef JSPropertyEnumeratorRetain(JSPropertyListEnumeratorRef enumerator);124 void JSPropertyEnumeratorRelease(JSProperty ListEnumeratorRef enumerator);125 JSCharBufferRef JSPropertyEnumeratorGetNext(JSContextRef context, JSProperty ListEnumeratorRef enumerator);135 JSPropertyEnumeratorRef JSObjectCreatePropertyEnumerator(JSContextRef context, JSObjectRef object); 136 JSPropertyEnumeratorRef JSPropertyEnumeratorRetain(JSPropertyEnumeratorRef enumerator); 137 void JSPropertyEnumeratorRelease(JSPropertyEnumeratorRef enumerator); 138 JSCharBufferRef JSPropertyEnumeratorGetNext(JSContextRef context, JSPropertyEnumeratorRef enumerator); 126 139 127 140 // Used for adding property names to a for...in enumeration -
trunk/JavaScriptCore/API/JSValueRef.cpp
r14951 r15133 25 25 */ 26 26 27 #include "APICast.h" 28 #include "JSCallbackObject.h" 27 29 #include "JSValueRef.h" 28 #include "APICast.h" 29 #include < JavaScriptCore/JSType.h>30 #include < JavaScriptCore/internal.h>31 #include < JavaScriptCore/operations.h>32 #include < JavaScriptCore/protect.h>33 #include < JavaScriptCore/ustring.h>34 #include < JavaScriptCore/value.h>30 31 #include <kjs/JSType.h> 32 #include <kjs/internal.h> 33 #include <kjs/operations.h> 34 #include <kjs/protect.h> 35 #include <kjs/ustring.h> 36 #include <kjs/value.h> 35 37 36 38 #include <wtf/Assertions.h> … … 98 100 } 99 101 102 bool JSValueIsObjectOfClass(JSValueRef value, JSClassRef jsClass) 103 { 104 JSValue* jsValue = toJS(value); 105 106 if (JSObject* o = jsValue->getObject()) 107 if (o->inherits(&JSCallbackObject::info)) 108 return static_cast<JSCallbackObject*>(o)->inherits(jsClass); 109 return false; 110 } 111 100 112 bool JSValueIsEqual(JSContextRef context, JSValueRef a, JSValueRef b) 101 113 { … … 124 136 } 125 137 138 bool JSValueIsInstanceOf(JSContextRef context, JSValueRef value, JSObjectRef object) 139 { 140 ExecState* exec = toJS(context); 141 JSValue* jsValue = toJS(value); 142 JSObject* jsObject = toJS(object); 143 if (!jsObject->implementsHasInstance()) 144 return false; 145 bool result = jsObject->hasInstance(exec, jsValue); 146 if (exec->hadException()) 147 exec->clearException(); 148 return result; 149 } 150 126 151 JSValueRef JSUndefinedMake() 127 152 { -
trunk/JavaScriptCore/API/JSValueRef.h
r15096 r15133 108 108 */ 109 109 bool JSValueIsObject(JSValueRef value); 110 bool JSValueIsObjectOfClass(JSValueRef value, JSClassRef jsClass); 110 111 111 112 // Comparing values … … 140 141 @result true if value is an instance of object 141 142 */ 142 bool JSValueIsInstanceOf(JS ValueRef value, JSObjectRef object);143 bool JSValueIsInstanceOf(JSContextRef context, JSValueRef value, JSObjectRef object); 143 144 144 145 // Creating values -
trunk/JavaScriptCore/API/minidom.c
r14951 r15133 26 26 27 27 #include "JavaScriptCore.h" 28 #include "JSNode.h" 28 29 #include <wtf/UnusedParam.h> 30 31 static char* createStringWithContentsOfFile(const char* fileName); 32 static JSValueRef print(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argc, JSValueRef argv[]); 29 33 30 34 int main(int argc, char* argv[]) … … 33 37 UNUSED_PARAM(argv); 34 38 39 JSContextRef context = JSContextCreate(NULL, NULL); 40 JSObjectRef globalObject = JSContextGetGlobalObject(context); 41 42 JSCharBufferRef printBuf = JSCharBufferCreateUTF8("print"); 43 JSObjectSetProperty(context, globalObject, printBuf, JSFunctionMake(context, print), kJSPropertyAttributeNone); 44 JSCharBufferRelease(printBuf); 45 46 JSCharBufferRef nodeBuf = JSCharBufferCreateUTF8("Node"); 47 JSObjectSetProperty(context, globalObject, nodeBuf, JSConstructorMake(context, JSNode_construct), kJSPropertyAttributeNone); 48 JSCharBufferRelease(nodeBuf); 49 50 char* script = createStringWithContentsOfFile("minidom.js"); 51 JSCharBufferRef scriptBuf = JSCharBufferCreateUTF8(script); 52 JSValueRef result; 53 JSEvaluate(context, globalObject, scriptBuf, NULL, 0, &result); 54 55 if (JSValueIsUndefined(result)) 56 printf("PASS: Test script executed successfully.\n"); 57 else { 58 printf("FAIL: Test script returned unexcpected value:\n"); 59 JSCharBufferRef resultBuf = JSValueCopyStringValue(context, result); 60 CFStringRef resultCF = CFStringCreateWithJSCharBuffer(kCFAllocatorDefault, resultBuf); 61 CFShow(resultCF); 62 CFRelease(resultCF); 63 JSCharBufferRelease(resultBuf); 64 } 65 JSCharBufferRelease(scriptBuf); 66 free(script); 67 68 #if 0 // used for leak/finalize debugging 69 int i; 70 for (i = 0; i < 1000; i++) { 71 JSObjectRef o = JSObjectMake(context, NULL, NULL); 72 (void)o; 73 } 74 JSGCCollect(); 75 #endif 76 77 JSContextDestroy(context); 78 printf("PASS: Program exited normally.\n"); 35 79 return 0; 36 80 } 81 82 static JSValueRef print(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argc, JSValueRef argv[]) 83 { 84 if (argc > 0) { 85 JSCharBufferRef stringBuf = JSValueCopyStringValue(context, argv[0]); 86 size_t numChars = JSCharBufferGetMaxLengthUTF8(stringBuf); 87 char string[numChars]; 88 JSCharBufferGetCharactersUTF8(stringBuf, string, numChars); 89 printf("%s\n", string); 90 } 91 92 return JSUndefinedMake(); 93 } 94 95 static char* createStringWithContentsOfFile(const char* fileName) 96 { 97 char* buffer; 98 99 int buffer_size = 0; 100 int buffer_capacity = 1024; 101 buffer = (char*)malloc(buffer_capacity); 102 103 FILE* f = fopen(fileName, "r"); 104 if (!f) { 105 fprintf(stderr, "Could not open file: %s\n", fileName); 106 return 0; 107 } 108 109 while (!feof(f) && !ferror(f)) { 110 buffer_size += fread(buffer + buffer_size, 1, buffer_capacity - buffer_size, f); 111 if (buffer_size == buffer_capacity) { // guarantees space for trailing '\0' 112 buffer_capacity *= 2; 113 buffer = (char*)realloc(buffer, buffer_capacity); 114 assert(buffer); 115 } 116 117 assert(buffer_size < buffer_capacity); 118 } 119 fclose(f); 120 buffer[buffer_size] = '\0'; 121 122 return buffer; 123 } -
trunk/JavaScriptCore/API/minidom.js
r14951 r15133 26 26 27 27 // For browser-based testing 28 if ( window) {28 if (typeof window != 'undefined') { 29 29 /* 30 30 The methods and constructors below approximate what that the native host should provide … … 60 60 } 61 61 62 Node.prototype. tag= "Node";62 Node.prototype.nodeType = "Node"; 63 63 64 64 Node.prototype.appendChild = function(child) { … … 74 74 75 75 printSpaces(numSpaces); 76 print('<' + this. tag+ '>' + '\n');76 print('<' + this.nodeType + '>' + '\n'); 77 77 78 78 var childNodesLength = this.childNodes.length; … … 81 81 82 82 printSpaces(numSpaces); 83 print('</' + this. tag+ '>\n');83 print('</' + this.nodeType + '>\n'); 84 84 } 85 85 … … 91 91 TextNode.prototype = new Node(); 92 92 93 TextNode.prototype. tag= "Text";93 TextNode.prototype.nodeType = "Text"; 94 94 95 95 TextNode.prototype.serialize = function(numSpaces) { … … 105 105 Element.prototype = new Node(); 106 106 107 Element.prototype. tag= "Element";107 Element.prototype.nodeType = "Element"; 108 108 109 109 RootElement = function() … … 113 113 RootElement.prototype = new Element(); 114 114 115 RootElement.prototype. tag= "Root";115 RootElement.prototype.nodeType = "Root"; 116 116 117 117 HeroElement = function() … … 121 121 HeroElement.prototype = new Element(); 122 122 123 HeroElement.prototype. tag= "Hero";123 HeroElement.prototype.nodeType = "Hero"; 124 124 125 125 VillainElement = function() … … 129 129 VillainElement.prototype = new Element(); 130 130 131 VillainElement.prototype. tag= "Villain";131 VillainElement.prototype.nodeType = "Villain"; 132 132 133 133 NameElement = function() … … 137 137 NameElement.prototype = new Element(); 138 138 139 NameElement.prototype. tag= "Name";139 NameElement.prototype.nodeType = "Name"; 140 140 141 141 WeaponElement = function() … … 145 145 WeaponElement.prototype = new Element(); 146 146 147 WeaponElement.prototype. tag= "Weapon";147 WeaponElement.prototype.nodeType = "Weapon"; 148 148 149 149 Document = function() … … 168 168 function test() 169 169 { 170 print("Node is " + Node); 171 for (var p in Node) 172 print(p + ": " + Node[p]); 173 174 node = new Node(); 175 print("node is " + node); 176 for (var p in node) 177 print(p + ": " + node[p]); 178 179 child1 = new Node(); 180 child2 = new Node(); 181 child3 = new Node(); 182 183 node.appendChild(child1); 184 node.appendChild(child2); 185 186 for (var i = 0; i < node.childNodes.length + 1; i++) { 187 print("item " + i + ": " + node.childNodes.item(i)); 188 } 189 190 for (var i = 0; i < node.childNodes.length + 1; i++) { 191 print(i + ": " + node.childNodes[i]); 192 } 193 194 node.removeChild(child1); 195 node.replaceChild(child3, child2); 196 197 for (var i = 0; i < node.childNodes.length + 1; i++) { 198 print("item " + i + ": " + node.childNodes.item(i)); 199 } 200 201 for (var i = 0; i < node.childNodes.length + 1; i++) { 202 print(i + ": " + node.childNodes[i]); 203 } 204 205 try { 206 node.appendChild(null); 207 } catch(e) { 208 print("caught: " + e); 209 } 210 211 try { 212 var o = new Object(); 213 o.appendChild = node.appendChild; 214 o.appendChild(node); 215 } catch(e) { 216 print("caught: " + e); 217 } 218 219 try { 220 node.appendChild(); 221 } catch(e) { 222 print("caught: " + e); 223 } 224 225 /* 170 226 var element, name, weapon; 171 227 … … 173 229 document.appendChild(document.createElement('Root')); 174 230 175 / * Tank Girl */231 // Tank Girl 176 232 element = document.createElement('Hero'); 177 233 … … 195 251 196 252 197 / * Skeletor */253 // Skeletor 198 254 element = document.createElement('Villain'); 199 255 … … 212 268 document.firstChild.appendChild(element); 213 269 214 / * Serialize */270 // Serialize 215 271 document.serialize(); 272 */ 216 273 } 274 275 test(); -
trunk/JavaScriptCore/API/testapi.c
r15043 r15133 108 108 CFRelease(expectedValueAsCFString); 109 109 110 assert(memcmp(jsBuffer, cfBuffer, cfLength * sizeof(UniChar)) == 0); 111 assert(jsLength == (size_t)cfLength); 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); 112 115 113 116 JSCharBufferRelease(valueAsString); … … 125 128 UNUSED_PARAM(object); 126 129 didInitialize = true; 127 }128 129 static JSCharBufferRef MyObject_copyDescription(JSObjectRef object)130 {131 UNUSED_PARAM(object);132 return JSCharBufferCreateUTF8("MyObject");133 130 } 134 131 … … 146 143 } 147 144 148 static bool MyObject_getProperty(JSObjectRef object, JSCharBufferRef propertyName, JSValueRef* returnValue) 149 { 145 static bool MyObject_getProperty(JSContextRef context, JSObjectRef object, JSCharBufferRef propertyName, JSValueRef* returnValue) 146 { 147 UNUSED_PARAM(context); 150 148 UNUSED_PARAM(object); 151 149 … … 252 250 } 253 251 254 JSObjectCallbacks MyObjectCallbacks = { 255 0, 252 JSObjectCallbacks MyObject_callbacks = { 256 253 0, 257 254 &MyObject_initialize, 258 255 &MyObject_finalize, 259 &MyObject_copyDescription,260 256 &MyObject_hasProperty, 261 257 &MyObject_getProperty, … … 268 264 }; 269 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 270 276 static JSValueRef print_callAsFunction(JSContextRef context, JSObjectRef functionObject, JSObjectRef thisObject, size_t argc, JSValueRef argv[]) 271 277 { … … 289 295 UNUSED_PARAM(constructorObject); 290 296 291 JSObjectRef result = JSObjectMake(context, &kJSObjectCallbacksNone, 0);297 JSObjectRef result = JSObjectMake(context, NULL, 0); 292 298 if (argc > 0) { 293 299 JSCharBufferRef valueBuffer = JSCharBufferCreateUTF8("value"); … … 306 312 UNUSED_PARAM(argv); 307 313 308 context = JSContextCreate( &kJSObjectCallbacksNone, 0);314 context = JSContextCreate(NULL, NULL); 309 315 310 316 JSValueRef jsUndefined = JSUndefinedMake(); … … 468 474 // GDB says jsGlobalValue actually ends up being marked by the stack crawl, so this 469 475 // exercise is a bit academic. Not sure why that happens, or how to avoid it. 470 jsGlobalValue = JSObjectMake(context, &kJSObjectCallbacksNone, 0);476 jsGlobalValue = JSObjectMake(context, NULL, 0); 471 477 JSGCCollect(); 472 478 assert(JSValueIsObject(jsGlobalValue)); … … 507 513 JSCharBufferRelease(badSyntaxBuf); 508 514 509 JSObjectRef myObject = JSObjectMake(context, &MyObjectCallbacks, 0); 515 JSCharBufferRef arrayBuf = JSCharBufferCreateUTF8("Array"); 516 JSValueRef v; 517 assert(JSObjectGetProperty(context, globalObject, arrayBuf, &v)); 518 JSObjectRef arrayConstructor = JSValueToObject(context, v); 519 JSCharBufferRelease(arrayBuf); 520 JSValueRef arrayObject; 521 assert(JSObjectCallAsConstructor(context, arrayConstructor, 0, NULL, &arrayObject)); 522 assert(JSValueIsInstanceOf(context, arrayObject, arrayConstructor)); 523 assert(!JSValueIsInstanceOf(context, JSNullMake(), arrayConstructor)); 524 525 JSObjectRef myObject = JSObjectMake(context, MyObject_class(context), NULL); 510 526 assert(didInitialize); 511 527 JSCharBufferRef myObjectBuf = JSCharBufferCreateUTF8("MyObject"); … … 538 554 539 555 // Allocate a few dummies so that at least one will be collected 540 JSObjectMake(context, &MyObjectCallbacks, 0);541 JSObjectMake(context, &MyObjectCallbacks, 0);556 JSObjectMake(context, MyObject_class(context), 0); 557 JSObjectMake(context, MyObject_class(context), 0); 542 558 JSGCCollect(); 543 559 assert(didFinalize); 544 560 545 561 JSContextDestroy(context); 546 printf("PASS: All assertions passed.\n");562 printf("PASS: Program exited normally.\n"); 547 563 return 0; 548 564 } -
trunk/JavaScriptCore/API/testapi.js
r15043 r15133 34 34 } 35 35 36 if (evalA == b || isNaN(evalA) && isNaN(b))36 if (evalA == b || isNaN(evalA) && typeof evalA == 'number' && isNaN(b) && typeof b == 'number') 37 37 print("PASS: " + a + " should be " + b + " and is.", "green"); 38 38 else … … 75 75 shouldBe("MyObject ? 1 : 0", 0); // toBoolean 76 76 shouldBe("+MyObject", 1); // toNumber 77 shouldBe("(MyObject + '').indexOf('MyObject') != -1", true); // toString77 shouldBe("(MyObject.toString())", "[object CallbackObject]"); // toString 78 78 shouldBe("MyObject - 0", NaN); // toPrimitive 79 79
Note:
See TracChangeset
for help on using the changeset viewer.