0

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.

A student
  • 170
  • 11

1 Answers1

0

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:

  • null, returns content of node.
  • content (string), sets content of node.
  • function, uses a function to set the content of the node.

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);
Diego Pino
  • 11,278
  • 1
  • 55
  • 57
  • I also want to make this work for the `add()` function, and the documentation says it can take a jQuery object. The error I am getting is `TypeError: unsupported type GObject` I was able to get the `html()` function working but don't know how to go about making the objects work as parameters – A student Dec 24 '19 at 17:56
  • Ok, I think that issue can get fixed if you cast `jsDiv` as a `JSC_TYPE_VALUE`. Try this: `jsc_value_object_invoke_method(jsVar, "html", JSC_TYPE_VALUE, jsDiv, G_TYPE_NONE);` – Diego Pino Dec 25 '19 at 18:24
  • It works now! Didn't know `JSC_TYPE_VALUE`was considered a `GType` – A student Dec 26 '19 at 01:11