Ignore:
Timestamp:
Mar 21, 2010, 12:40:04 AM (15 years ago)
Author:
[email protected]
Message:

2010-03-20 Oliver Hunt <[email protected]>

Reviewed by Maciej Stachowiak.

JSC needs an API to allow custom objects to have aprivate GC-accessible properties
https://bugs.webkit.org/show_bug.cgi?id=36420

Add new API methods to support "private" properties on custom
objects.

  • API/JSCallbackObject.h: (JSC::JSCallbackObjectData::JSCallbackObjectData): (JSC::JSCallbackObjectData::~JSCallbackObjectData): (JSC::JSCallbackObjectData::getPrivateProperty): (JSC::JSCallbackObjectData::setPrivateProperty): (JSC::JSCallbackObjectData::deletePrivateProperty): (JSC::JSCallbackObjectData::markChildren): (JSC::JSCallbackObjectData::JSPrivatePropertyMap::getPrivateProperty): (JSC::JSCallbackObjectData::JSPrivatePropertyMap::setPrivateProperty): (JSC::JSCallbackObjectData::JSPrivatePropertyMap::deletePrivateProperty): (JSC::JSCallbackObjectData::JSPrivatePropertyMap::markChildren): (JSC::JSCallbackObject::getPrivateProperty): (JSC::JSCallbackObject::setPrivateProperty): (JSC::JSCallbackObject::deletePrivateProperty): (JSC::JSCallbackObject::markChildren):
  • API/JSObjectRef.cpp: (JSObjectGetPrivateProperty): (JSObjectSetPrivateProperty): (JSObjectDeletePrivateProperty):
  • API/JSObjectRefPrivate.h: Added.
  • API/tests/testapi.c: (main):
  • JavaScriptCore.exp:
  • JavaScriptCore.xcodeproj/project.pbxproj:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/API/tests/testapi.c

    r56189 r56314  
    4343#endif
    4444
     45bool JSObjectSetPrivateProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef value);
     46JSValueRef JSObjectGetPrivateProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName);
     47bool JSObjectDeletePrivateProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName);
     48
    4549static JSGlobalContextRef context;
    4650static int failed;
     
    754758
    755759static JSValueRef jsNumberValue =  NULL;
     760
     761static JSObjectRef aHeapRef = NULL;
    756762
    757763static void makeGlobalNumberValue(JSContextRef context) {
     
    870876    JSObjectSetProperty(context, globalObject, EmptyObjectIString, EmptyObject, kJSPropertyAttributeNone, NULL);
    871877    JSStringRelease(EmptyObjectIString);
    872 
     878   
     879    JSStringRef lengthStr = JSStringCreateWithUTF8CString("length");
     880    aHeapRef = JSObjectMakeArray(context, 0, 0, 0);
     881    JSObjectSetProperty(context, aHeapRef, lengthStr, JSValueMakeNumber(context, 10), 0, 0);
     882    JSStringRef privatePropertyName = JSStringCreateWithUTF8CString("privateProperty");
     883    if (!JSObjectSetPrivateProperty(context, myObject, privatePropertyName, aHeapRef)) {
     884        printf("FAIL: Could not set private property.\n");
     885        failed = 1;       
     886    } else {
     887        printf("PASS: Set private property.\n");
     888    }
     889    if (JSObjectSetPrivateProperty(context, aHeapRef, privatePropertyName, aHeapRef)) {
     890        printf("FAIL: JSObjectSetPrivateProperty should fail on non-API objects.\n");
     891        failed = 1;       
     892    } else {
     893        printf("PASS: Did not allow JSObjectSetPrivateProperty on a non-API object.\n");
     894    }
     895    if (JSObjectGetPrivateProperty(context, myObject, privatePropertyName) != aHeapRef) {
     896        printf("FAIL: Could not retrieve private property.\n");
     897        failed = 1;
     898    } else
     899        printf("PASS: Retrieved private property.\n");
     900    if (JSObjectGetPrivateProperty(context, aHeapRef, privatePropertyName)) {
     901        printf("FAIL: JSObjectGetPrivateProperty should return NULL when called on a non-API object.\n");
     902        failed = 1;
     903    } else
     904        printf("PASS: JSObjectGetPrivateProperty return NULL.\n");
     905   
     906    if (JSObjectGetProperty(context, myObject, privatePropertyName, 0) == aHeapRef) {
     907        printf("FAIL: Accessed private property through ordinary property lookup.\n");
     908        failed = 1;
     909    } else
     910        printf("PASS: Cannot access private property through ordinary property lookup.\n");
     911   
     912    JSGarbageCollect(context);
     913   
     914    for (int i = 0; i < 10000; i++)
     915        JSObjectMake(context, 0, 0);
     916
     917    if (JSValueToNumber(context, JSObjectGetProperty(context, aHeapRef, lengthStr, 0), 0) != 10) {
     918        printf("FAIL: Private property has been collected.\n");
     919        failed = 1;
     920    } else
     921        printf("PASS: Private property does not appear to have been collected.\n");
     922    JSStringRelease(lengthStr);
     923   
    873924    JSStringRef validJSON = JSStringCreateWithUTF8CString("{\"aProperty\":true}");
    874925    JSValueRef jsonObject = JSValueMakeFromJSONString(context, validJSON);
Note: See TracChangeset for help on using the changeset viewer.