Args in ExecuteFunctionWithContext

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

Args in ExecuteFunctionWithContext

Postby nyu » Thu Feb 20, 2014 7:45 pm

I'm trying to make an asynchronous function in CEF, everything works well without passing args to ExecuteFunctionWithContext.

I have this class

Code: Select all
class SendTask : public CefTask{
  public:
    SendTask(V8Callback p, CefV8ValueList a, CefRefPtr<CefV8Value> r){
        param = p;
        args = a;
        retval = r;
    }
    void Execute(){
        try{
            CefRefPtr<CefV8Value> ret = param.func->ExecuteFunctionWithContext(param.context, retval, args);
            if (ret) {
                debug(":)");
            } else {
                debug(":(");
            }

        } catch (std::exception& e){
            debug( e.what() );
        }
    }
    int AddRef(){}
    int Release(){}
    int GetRefCt(){}
  private:
    V8Callback param;
    CefV8ValueList args;
    CefRefPtr<CefV8Value> retval;
};


I'm seeing that 'ret' is NULL, so the function is failing.

I'm calling this from a function
Code: Select all
void example_callback(V8Callback param){
    CefV8ValueList args;
    CefRefPtr<CefV8Exception> exception;
    args.push_back(CefV8Value::CreateInt(1)); // <--- this fails
    CefRefPtr<CefV8Value> retval = CefV8Value::CreateInt(99);
    param.taskrunner->PostTask(new SendTask(param, args, retval));
}


Everything works well until i push a value into args, if i comment that line, i see the alert

The js is as simple as

Code: Select all
function myFunc() {
   for (var i = arguments.length - 1; i >= 0; i--) {
      alert(arguments[i]);
   };
        alert('works');
   return true;
}
window.register(myFunc);


I can't find good documentation about this function, what could be happening there? :?:
nyu
Newbie
 
Posts: 5
Joined: Thu Feb 20, 2014 7:33 pm

Re: Args in ExecuteFunctionWithContext

Postby magreenblatt » Thu Feb 20, 2014 7:53 pm

The second argument to ExecuteFunctionWithContext is the |this| object. That doesn't match your usage of the |retval| variable. Try simplifying your code to verify correct usage of ExecuteFunctionWithContext before adding additional complexity.
magreenblatt
Site Admin
 
Posts: 12383
Joined: Fri May 29, 2009 6:57 pm

Re: Args in ExecuteFunctionWithContext

Postby nyu » Fri Feb 21, 2014 9:58 am

Actually i was just testing it, it doesn't matter what i put there:

This works and i see the alert:

Code: Select all
void example_callback(V8Callback param){
    CefV8ValueList args;
    CefRefPtr<CefV8Value> retval;
    param.taskrunner->PostTask(new SendTask(param, args, retval));
}


This doesn't work, the callback doesn't get executed:

Code: Select all
void example_callback(V8Callback param){
    CefV8ValueList args;
    args.push_back(CefV8Value::CreateInt(1)); // <--
    CefRefPtr<CefV8Value> retval;
    param.taskrunner->PostTask(new SendTask(param, args, retval));
}


And i can't debug in CEF so i'm in a Trial and error situation.
I assert( ret != NULL) and it fails, how could i know what went wrong in ExecuteFunctionWithContext?
nyu
Newbie
 
Posts: 5
Joined: Thu Feb 20, 2014 7:33 pm

Re: Args in ExecuteFunctionWithContext

Postby magreenblatt » Fri Feb 21, 2014 1:08 pm

What thread is |example_callback| running on? If you're creating V8 objects on a thread other than the renderer process main thread that could be the problem.
magreenblatt
Site Admin
 
Posts: 12383
Joined: Fri May 29, 2009 6:57 pm

Re: Args in ExecuteFunctionWithContext

Postby nyu » Fri Feb 21, 2014 2:15 pm

Oh i see, i'm doing it in a boost thread:

Code: Select all
if (name == "register") {

        if (arguments.size() == 1 && arguments[0]->IsFunction()) {

            CefRefPtr<CefV8Value> callback_func_ = arguments[0];
            CefRefPtr<CefV8Context> callback_context_ = CefV8Context::GetCurrentContext();
            CefRefPtr<CefTaskRunner> task_runner = CefTaskRunner::GetForCurrentThread();
            V8Callback param = {task_runner, callback_func_, callback_context_};
            boost::thread myThread( example_callback, param );

            handled = true;
        }
    }


I need to generate the output in a thread so it doesn't block the UI, how could i create the objects in the main thread.

UPDATE:

It was my mistake, example_callback is in a thread where i was creating the objects, moved them to the CefTask made it work

Code: Select all
    void Execute(){
        try{
            CefV8ValueList args;
            args.push_back(CefV8Value::CreateInt(1));
            CefRefPtr<CefV8Value> retval;
            CefRefPtr<CefV8Value> ret = param.func->ExecuteFunctionWithContext(param.context, retval, args);
            if (ret) {
                debug(":)");
            } else {
                debug(":(");
            }

        } catch (std::exception& e){
            debug( e.what() );
        }
    }


Thank you very much. :D
nyu
Newbie
 
Posts: 5
Joined: Thu Feb 20, 2014 7:33 pm

Re: Args in ExecuteFunctionWithContext

Postby nyu » Mon Feb 24, 2014 11:05 am

Actually it isn't resolved yet, my last test was only with CefV8Value::CreateString(""), CefV8Value::CreateInt(2).
But when i try to use CefV8Value::CreateArray(100), CefV8Value::CreateObject(NULL) it fails.

Basically i'm creating a boost::thread, doing some process in it, then sending the result to a CefTask so i can get in the UI thread and get the result in javascript callback. I can send primitives like integers or strings but not array or objects.

I don't understand why, why creating an array is different than creating a int in CEF.
nyu
Newbie
 
Posts: 5
Joined: Thu Feb 20, 2014 7:33 pm

Re: Args in ExecuteFunctionWithContext

Postby magreenblatt » Mon Feb 24, 2014 11:25 am

Read the comments on the methods. You need to be in a V8 context when creating objects (functions, arrays, etc).
magreenblatt
Site Admin
 
Posts: 12383
Joined: Fri May 29, 2009 6:57 pm

Re: Args in ExecuteFunctionWithContext

Postby nyu » Mon Feb 24, 2014 11:28 am

Yeah sorry, i already found out that, i needed to call context->Enter(), and context->Exit() before and after creating them, and now it works like intended. Thank you very much. :D
nyu
Newbie
 
Posts: 5
Joined: Thu Feb 20, 2014 7:33 pm

Re: Args in ExecuteFunctionWithContext

Postby prsolucoes » Mon Jun 28, 2021 12:12 am

Hi,

Im with the same problem here:
viewtopic.php?f=6&t=18483

How do you solve yours?
prsolucoes
Techie
 
Posts: 10
Joined: Sat Jun 19, 2021 3:20 am


Return to Support Forum

Who is online

Users browsing this forum: No registered users and 48 guests