UString::Rep* nameRep = toJS(propertyName);
JSValue* jsValue = jsObject->get(exec, Identifier(nameRep));
- if (jsValue->isUndefined())
- jsValue = 0;
if (exec->hadException()) {
if (exception)
*exception = toRef(exec->exception());
}
}
-JSValueRef JSObjectGetPropertyAtIndex(JSContextRef context, JSObjectRef object, unsigned propertyIndex)
+JSValueRef JSObjectGetPropertyAtIndex(JSContextRef context, JSObjectRef object, unsigned propertyIndex, JSValueRef* exception)
{
JSLock lock;
ExecState* exec = toJS(context);
JSObject* jsObject = toJS(object);
JSValue* jsValue = jsObject->get(exec, propertyIndex);
- if (jsValue->isUndefined())
- return 0;
+ if (exec->hadException()) {
+ if (exception)
+ *exception = toRef(exec->exception());
+ exec->clearException();
+ }
return toRef(jsValue);
}
-void JSObjectSetPropertyAtIndex(JSContextRef context, JSObjectRef object, unsigned propertyIndex, JSValueRef value)
+void JSObjectSetPropertyAtIndex(JSContextRef context, JSObjectRef object, unsigned propertyIndex, JSValueRef value, JSValueRef* exception)
{
JSLock lock;
ExecState* exec = toJS(context);
JSValue* jsValue = toJS(value);
jsObject->put(exec, propertyIndex, jsValue);
+ if (exec->hadException()) {
+ if (exception)
+ *exception = toRef(exec->exception());
+ exec->clearException();
+ }
}
bool JSObjectDeleteProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
@param object The JSObject whose property you want to get.
@param propertyName A JSString containing the property's name.
@param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception.
-@result The property's value if object has the property, otherwise NULL.
+@result The property's value if object has the property, otherwise the undefined value.
*/
JSValueRef JSObjectGetProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception);
@param context The execution context to use.
@param object The JSObject whose property you want to get.
@param propertyIndex The property's name as a number
-@result The property's value if object has the property, otherwise NULL.
-@discussion This is equivalent to getting a property by a string name containing the number, but allows faster access to JS arrays.
+@param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception.
+@result The property's value if object has the property, otherwise the undefined value.
+@discussion Calling JSObjectGetPropertyAtIndex is equivalent to calling JSObjectGetProperty with a string containing propertyIndex, but it enables optimized access to JavaScript arrays.
*/
-JSValueRef JSObjectGetPropertyAtIndex(JSContextRef context, JSObjectRef object, unsigned propertyIndex);
+JSValueRef JSObjectGetPropertyAtIndex(JSContextRef context, JSObjectRef object, unsigned propertyIndex, JSValueRef* exception);
/*!
@function
@param object The JSObject whose property you want to set.
@param propertyIndex The property's name as a number
@param value A JSValue to use as the property's value.
-@discussion This is equivalent to setting a property by a string name containing the number, but allows faster access to JS arrays.
+@param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception.
+@discussion Calling JSObjectSetPropertyAtIndex is equivalent to calling JSObjectSetProperty with a string containing propertyIndex, but it enables optimized access to JavaScript arrays.
*/
-void JSObjectSetPropertyAtIndex(JSContextRef context, JSObjectRef object, unsigned propertyIndex, JSValueRef value);
+void JSObjectSetPropertyAtIndex(JSContextRef context, JSObjectRef object, unsigned propertyIndex, JSValueRef value, JSValueRef* exception);
/*!
@function
if (JSStringIsEqualToUTF8CString(propertyName, "alwaysOne")
|| JSStringIsEqualToUTF8CString(propertyName, "cantFind")
|| JSStringIsEqualToUTF8CString(propertyName, "myPropertyName")
- || JSStringIsEqualToUTF8CString(propertyName, "hasPropertyLie")) {
+ || JSStringIsEqualToUTF8CString(propertyName, "hasPropertyLie")
+ || JSStringIsEqualToUTF8CString(propertyName, "0")) {
return true;
}
return JSValueMakeUndefined();
}
+ if (JSStringIsEqualToUTF8CString(propertyName, "0")) {
+ *exception = JSValueMakeNumber(1);
+ return JSValueMakeNumber(1);
+ }
+
return NULL;
}
exception = NULL;
assert(!JSValueIsEqual(context, jsObjectNoProto, JSValueMakeNumber(1), &exception));
assert(exception);
+
+ exception = NULL;
+ JSObjectGetPropertyAtIndex(context, myObject, 0, &exception);
+ assert(1 == JSValueToNumber(context, exception, NULL));
assertEqualsAsBoolean(jsUndefined, false);
assertEqualsAsBoolean(jsNull, false);
assert(JSValueIsObject(exception));
JSStringRef array = JSStringCreateWithUTF8CString("Array");
- v = JSObjectGetProperty(context, globalObject, array, NULL);
- assert(v);
- JSObjectRef arrayConstructor = JSValueToObject(context, v, NULL);
+ JSObjectRef arrayConstructor = JSValueToObject(context, JSObjectGetProperty(context, globalObject, array, NULL), NULL);
JSStringRelease(array);
result = JSObjectCallAsConstructor(context, arrayConstructor, 0, NULL, NULL);
assert(result);
+ assert(JSValueIsObject(result));
assert(JSValueIsInstanceOfConstructor(context, result, arrayConstructor, NULL));
assert(!JSValueIsInstanceOfConstructor(context, JSValueMakeNull(), arrayConstructor, NULL));
+
+ o = JSValueToObject(context, result, NULL);
+ exception = NULL;
+ assert(JSValueIsUndefined(JSObjectGetPropertyAtIndex(context, o, 0, &exception)));
+ assert(!exception);
+ JSObjectSetPropertyAtIndex(context, o, 0, JSValueMakeNumber(1), &exception);
+ assert(!exception);
+
+ exception = NULL;
+ assert(1 == JSValueToNumber(context, JSObjectGetPropertyAtIndex(context, o, 0, &exception), &exception));
+ assert(!exception);
+
JSStringRef functionBody;
JSObjectRef function;
assert(!JSObjectMakeFunctionWithBody(context, NULL, 0, NULL, functionBody, NULL, 1, &exception));
assert(JSValueIsObject(exception));
v = JSObjectGetProperty(context, JSValueToObject(context, exception, NULL), line, NULL);
- assert(v);
assertEqualsAsNumber(v, 2); // FIXME: Lexer::setCode bumps startingLineNumber by 1 -- we need to change internal callers so that it doesn't have to (saying '0' to mean '1' in the API would be really confusing -- it's really confusing internally, in fact)
JSStringRelease(functionBody);
JSStringRelease(line);
v = JSObjectCallAsFunction(context, function, o, 0, NULL, NULL);
assert(JSValueIsEqual(context, v, o, NULL));
+
+
char* scriptUTF8 = createStringWithContentsOfFile("testapi.js");
JSStringRef script = JSStringCreateWithUTF8CString(scriptUTF8);
result = JSEvaluateScript(context, script, NULL, NULL, 1, &exception);
+2006-07-16 Geoffrey Garen <ggaren@apple.com>
+
+ Reviewed by Maciej.
+
+ - Change getProperty* to return undefined, rather than NULL, for missing
+ properties, since that's what the spec says. Also added exception out
+ parameters to the *Index functions, because they can call through to the
+ regular functions, which can throw for custom objects.
+
+ * API/JSObjectRef.cpp:
+ (JSObjectGetProperty):
+ (JSObjectGetPropertyAtIndex):
+ (JSObjectSetPropertyAtIndex):
+ * API/JSObjectRef.h:
+ * API/testapi.c:
+ (main):
+
2006-07-16 Geoffrey Garen <ggaren@apple.com>
Reviewed by Maciej.