Skip to content
This repository has been archived by the owner on May 6, 2019. It is now read-only.

Commit

Permalink
Added TryInvoke dynamic binding function to JSValue
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Nelson <peter@peterdn.com>
  • Loading branch information
peterdn committed Mar 28, 2013
1 parent 423582a commit 5a67dae
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 13 deletions.
15 changes: 5 additions & 10 deletions JSCore/JSObject.cpp
Expand Up @@ -72,29 +72,24 @@ void JSObject::SetProperty(String ^ propertyName, ActionDelegate ^ func)
SetProperty(propertyName, jsVal);
}

JSValue ^ JSObject::CallAsFunction(JSContext ^ context, array<Object ^> ^ variableArgs)
JSValue ^ JSObject::CallAsFunction(array<Object ^> ^ variableArgs)
{
JSContextRef ctx = _context->context();
JSContextRef ctxworks = context->context();

if (ctx == ctxworks) {
float i = 2;
}

JSValueRef * args = new JSValueRef[variableArgs->Length];
for(int i = 0; i < variableArgs->Length; i++)
{
args[i] = getJSValueRefFromObject(context->context(), variableArgs[i], NULL);
args[i] = getJSValueRefFromObject(ctx, variableArgs[i], NULL);
}

JSValueRef ret = JSObjectCallAsFunction(context->context(), (JSObjectRef)_value, NULL, variableArgs->Length, args, NULL);
JSValueRef ret = JSObjectCallAsFunction(ctx, (JSObjectRef)_value, NULL, variableArgs->Length, args, NULL);

for(int i = 0; i < variableArgs->Length; i++)
{
JSValueUnprotect(context->context(), args[i]);
JSValueUnprotect(ctx, args[i]);
}
delete[] args;
return gcnew JSValue(context, ret);
return gcnew JSValue(_context, ret);
}

JSValue ^ JSObject::CallFunction(String ^ methodName, ... array<Object ^> ^ variableArgs)
Expand Down
2 changes: 1 addition & 1 deletion JSCore/JSObject.h
Expand Up @@ -31,7 +31,7 @@ public ref class JSObject : public WebKit::JSCore::JSValue
void SetProperty(String ^ propertyName, System::String ^ value);
void SetProperty(String ^ propertyName, EventDelegate ^ func);
void SetProperty(String ^ propertyName, ActionDelegate ^ func);
JSValue ^ JSObject::CallAsFunction(JSContext ^ context, array<Object ^> ^ variableArgs);
JSValue ^ CallAsFunction(array<Object ^> ^ variableArgs);
JSValue ^ CallFunction(String ^ methodName, ... array<Object ^> ^ variableArgs);

Dictionary<Object^, Object^>^ ToDictionary();
Expand Down
9 changes: 9 additions & 0 deletions JSCore/JSValue.cpp
Expand Up @@ -188,5 +188,14 @@ bool JSValue::TryInvokeMember(System::Dynamic::InvokeMemberBinder ^ binder, arra
result = obj->CallFunction(binder->Name, args);
return true;
}

bool JSValue::TryInvoke(System::Dynamic::InvokeBinder^ binder, array<Object ^> ^ args, [OutAttribute] Object ^% result)
{
if (!IsObject) return false;
JSObject ^ obj = ToObject();
result = obj->CallAsFunction(args);
return true;
}

#endif

1 change: 1 addition & 0 deletions JSCore/JSValue.h
Expand Up @@ -51,6 +51,7 @@ public ref class JSValue
virtual bool TryGetMember(System::Dynamic::GetMemberBinder^ binder, [OutAttribute] Object ^% result) override;
virtual bool TrySetMember(System::Dynamic::SetMemberBinder^ binder, Object ^ result) override;
virtual bool TryInvokeMember(System::Dynamic::InvokeMemberBinder^ binder, array<Object ^> ^ args, [OutAttribute] Object ^% result) override;
virtual bool TryInvoke(System::Dynamic::InvokeBinder^ binder, array<Object ^> ^ args, [OutAttribute] Object ^% result) override;
#endif

internal:
Expand Down
8 changes: 6 additions & 2 deletions WebKitBrowserTest/MainForm.cs
Expand Up @@ -291,13 +291,17 @@ private void jSTestPageToolStripMenuItem_Click(object sender, EventArgs e)

private void test3ToolStripMenuItem_Click(object sender, EventArgs e)
{
JSContext ctx = (JSContext)currentPage.browser.GetGlobalScriptContext();
JSContext ctx = (JSContext) currentPage.browser.GetGlobalScriptContext();
var window = ctx.GetGlobalObject();
dynamic doStuff = window.GetProperty("doStuff");
doStuff("hello!");
/*JSContext ctx = (JSContext)currentPage.browser.GetGlobalScriptContext();
TestClass myTest = new TestClass()
{
ctx = ctx
};
currentPage.browser.ObjectForScripting = myTest;
currentPage.browser.ObjectForScripting = myTest;*/

//ctx.EvaluateScript("test()");

Expand Down

0 comments on commit 5a67dae

Please sign in to comment.