I am trying to call some jQuery functions from C++ code. What I did was copy the entire jQuery library as a string and used jsc_context_evaluate() to get access to all the jQuery methods.
Here is an unspecific stripped-down version with a string, it works fine:
JSCValue* jsVar = jsc_context_evaluate(jsCtx, "$('some-div')");
jsc_value_object_invoke_method(jsVar, "html", G_TYPE_STRING, "foo", G_TYPE_NONE);
This changes the contents of 'some-div' to just "foo". (I know 'some-div' should really have a '.' or '#' but it is just for representation)
When I try to use an object, the function doesn't work.
JSCValue* jsVar = jsc_context_evaluate(jsCtx, "$('some-div')");
JSCValue* jsDiv = jsc_context_evaluate(jsCtx, "$('some-other-div')");
jsc_value_object_invoke_method(jsVar, "html", G_TYPE_OBJECT, jsDiv, G_TYPE_NONE);
Instead of 'some-div' getting the content of 'some-other-div', nothing happens. When I print the return value of ...invoke_method()
as a string with jsc_value_to_string()
I get undefined. I tried it with other jQuery methods like add
but it's the same result, the function returns undefined and nothing changes.
The first thing I would do is to check whether the function calls are not emitting an exception. Apparently jsc_context_evaluate
is working, so I just check the last function call:
JSCValue* jsVar = jsc_context_evaluate(jsCtx, "$('some-div')");
JSCValue* jsDiv = jsc_context_evaluate(jsCtx, "$('some-other-div')");
jsc_value_object_invoke_method(jsVar, "html", G_TYPE_OBJECT, jsDiv, G_TYPE_NONE);
JSCException* exception = jsc_context_get_exception(jsCtx);
if (exception != NULL) {
g_print("%s: %s\n", jsc_exception_get_name(exception),
jsc_exception_get_message(exception));
free(exception);
return;
}
That may give you a hint of what's going on.
My suspicion is that the problem is that you're passing an object to jQuery's function html
. According to jQuery's doc (https://www.geeksforgeeks.org/jquery-html-method/), the html
function can receive 3 types of parameters:
jsDiv
is an object. I think you would need to pass its content, something like this:
JSCValue* jsVar = jsc_context_evaluate(jsCtx, "$('some-div')");
JSCValue* jsDiv = jsc_context_evaluate(jsCtx, "$('some-other-div')");
JSCValue* content = jsc_value_object_invoke_method(jsDiv, "html", G_TYPE_NONE);
char* contentString = jsc_value_to_string(jsCtx, content);
jsc_value_object_invoke_method(jsVar, "html", G_TYPE_OBJECT, contentString, G_TYPE_NONE);