[ create a new paste ] login | about

Link: http://codepad.org/6CrB6Fq1    [ raw code | fork ]

C, pasted on Jan 28:
void Invoke_Script(char *script_name, int argc, ...) {
	
	IWebFrame* frame = 0;	
	JSContextRef context;
	JSObjectRef globalObject,functionObject;
	JSValueRef *arguments=NULL;
	JSStringRef functionName,argStr;
	
	HRESULT hr;
	va_list ap;
	int i;
	char *script_arg;

	//Get the Web Frame
	hr = gWebView->mainFrame(&frame);
	if (FAILED(hr))
        goto exit;
	
	//Get the Context Ref
	context = frame->globalContext();

	//Get the Global Object
	globalObject = JSContextGetGlobalObject(context);
	
	//Get the Function Name in JSString
	functionName = JSStringCreateWithUTF8CString(script_name);
	
	//Get the Arguments
	va_start(ap,argc);

	if(argc>0)
		arguments = (JSValueRef*)GlobalAlloc(GMEM_FIXED,sizeof(JSValueRef)*argc);

	for (i=argc-1;i>=0;i--) {
		
		script_arg=va_arg(ap,char*);	
		
		argStr = JSStringCreateWithUTF8CString(script_arg);
		arguments[i] = JSValueMakeString(context, argStr);		
		JSStringRelease(argStr);

		script_arg+=strlen(script_arg)+1;		
	}
	
	va_end(script_arg);

	//Get a reference to your Javascript function
	functionObject = (JSObjectRef)JSObjectGetProperty(context, globalObject, functionName, NULL);
	
	//Call the JS Function
	JSObjectCallAsFunction(context, functionObject, globalObject, argc, arguments, NULL);
	
	JSStringRelease(functionName);
	GlobalFree(arguments);

exit:
	if (frame)
		frame->Release();			
}


Create a new paste based on this one


Comments: