static JSValueRef JSNode_removeChild(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) { UNUSED_PARAM(function); /* Example of ignoring invalid values */ if (argumentCount > 0) { if (JSValueIsObjectOfClass(context, thisObject, JSNode_class(context))) { if (JSValueIsObjectOfClass(context, arguments[0], JSNode_class(context))) { Node* node = JSObjectGetPrivate(thisObject); Node* child = JSObjectGetPrivate(JSValueToObject(context, arguments[0], exception)); Node_removeChild(node, child); } } } return JSValueMakeUndefined(context); }
static JSValueRef JSNode_replaceChild(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) { UNUSED_PARAM(function); if (argumentCount > 1) { if (JSValueIsObjectOfClass(context, thisObject, JSNode_class(context))) { if (JSValueIsObjectOfClass(context, arguments[0], JSNode_class(context))) { if (JSValueIsObjectOfClass(context, arguments[1], JSNode_class(context))) { Node* node = JSObjectGetPrivate(thisObject); Node* newChild = JSObjectGetPrivate(JSValueToObject(context, arguments[0], exception)); Node* oldChild = JSObjectGetPrivate(JSValueToObject(context, arguments[1], exception)); Node_replaceChild(node, newChild, oldChild); } } } } return JSValueMakeUndefined(context); }
static JSValueRef JSNode_appendChild(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) { UNUSED_PARAM(function); /* Example of throwing a type error for invalid values */ if (!JSValueIsObjectOfClass(context, thisObject, JSNode_class(context))) { JSStringRef message = JSStringCreateWithUTF8CString("TypeError: appendChild can only be called on nodes"); *exception = JSValueMakeString(context, message); JSStringRelease(message); } else if (argumentCount < 1 || !JSValueIsObjectOfClass(context, arguments[0], JSNode_class(context))) { JSStringRef message = JSStringCreateWithUTF8CString("TypeError: first argument to appendChild must be a node"); *exception = JSValueMakeString(context, message); JSStringRelease(message); } else { Node* node = JSObjectGetPrivate(thisObject); Node* child = JSObjectGetPrivate(JSValueToObject(context, arguments[0], NULL)); Node_appendChild(node, child); } return JSValueMakeUndefined(context); }
JSObjectRef JSNode_new(JSContextRef context, Node* node) { return JSObjectMake(context, JSNode_class(context), node); }
int main(int argc, char* argv[]) { const char *scriptPath = "minidom.js"; if (argc > 1) { scriptPath = argv[1]; } AJGlobalContextRef context = AJGlobalContextCreateInGroup(NULL, NULL); AJObjectRef globalObject = AJContextGetGlobalObject(context); AJStringRef printIString = AJStringCreateWithUTF8CString("print"); AJObjectSetProperty(context, globalObject, printIString, AJObjectMakeFunctionWithCallback(context, printIString, print), kAJPropertyAttributeNone, NULL); AJStringRelease(printIString); AJStringRef node = AJStringCreateWithUTF8CString("Node"); AJObjectSetProperty(context, globalObject, node, AJObjectMakeConstructor(context, JSNode_class(context), JSNode_construct), kAJPropertyAttributeNone, NULL); AJStringRelease(node); char* scriptUTF8 = createStringWithContentsOfFile(scriptPath); AJStringRef script = AJStringCreateWithUTF8CString(scriptUTF8); AJValueRef exception; AJValueRef result = AJEvaluateScript(context, script, NULL, NULL, 1, &exception); if (result) printf("PASS: Test script executed successfully.\n"); else { printf("FAIL: Test script threw exception:\n"); AJStringRef exceptionIString = AJValueToStringCopy(context, exception, NULL); size_t exceptionUTF8Size = AJStringGetMaximumUTF8CStringSize(exceptionIString); char* exceptionUTF8 = (char*)malloc(exceptionUTF8Size); AJStringGetUTF8CString(exceptionIString, exceptionUTF8, exceptionUTF8Size); printf("%s\n", exceptionUTF8); free(exceptionUTF8); AJStringRelease(exceptionIString); } AJStringRelease(script); free(scriptUTF8); globalObject = 0; AJGlobalContextRelease(context); printf("PASS: Program exited normally.\n"); return 0; }