Hi,

I have a problem about v8. I should make a timer class, what would has
a SetTimeout function. But I don't understand how should I write that.
Unfortunately I'm not allowed to use node.js .

For example I want to use a script like this :
"function f() {
    var timer1 = new Timer();
    var timer2 = new Timer();
    timer1.SetTimeout(function g() {...}, 5);
    timer1.SetTimeout(function h() {...}, 20);
}"

How would g() be a  callback in this code;(or why it is  callback?)->I
mean why the first argument of SetTimeout is a callback?
How do callbacks work in v8? and How should I use it in the SetTimeout
function? I haven't found any example for this.
If somebody knows a file in v8  where the callback is realized then
please write a message.

Now, my version looks like this:

I have 2 c++ classes. The first is a Runner class in c++(it has a
SetTimeout and Cancel methods). In the SetTimeout takes some
seconds(this is the second argument, the first is Task) and after that
it calls the Executes of Task.  The second class(Task) is in c++ too,
what has just an Execution function.

I have another class(it's name is V8), which uses v8. It has a Compile
and a Run methods. I wrapped a class(let's call v8 Runner), which call
Runner inside :

this is a v8 Runner's Execute  :
v8::Handle<v8::Value> Runner::Execute(const v8::Arguments &args) {

        v8::HandleScope scope;
        v8::Context::Scope context_scope(context_);

        runner_ = new Runner();
        int sleep_time;

        if(2 != args.Length()) {
                return scope.Close(v8::Undefined());
        } else {
                if(args[0]->IsFunction()){
                        sleep_time = args[1]->Int32Value();

                        v8::Local<v8::Function> f = 
v8::Local<v8::Function>::Cast(args[0]);
                        v8::Persistent<v8::Function> function =
v8::Persistent<v8::Function>::New(f);

                        task_ = new Task(context_, function);
                        runner_->Execute(task_, (int)sleep_time);
                } else {
                        return scope.Close(v8::Undefined());
                }
        }

        return scope.Close(v8::Undefined());
}
Task gets a context and a function. The function does not return
anything. The function will be called by Task. I think this solution
is  not so good. Can I use the Runner and the Task class, if I would
like to solve this problem with callback?


The Runner c++ class works fine.
There is a Task's Execute method

void Task::Execute(){
        int number;
        v8::HandleScope scope;
        v8::Context::Scope context_scope(context_);
        v8::TryCatch try_catch;

        int counter = 1;

        v8::Handle<v8::Value> result = function_->Call(context_->Global(),
NULL, NULL);
        if(result.IsEmpty()) {
                v8::Handle<v8::Value> exception = try_catch.Exception();
                v8::String::AsciiValue asci(exception);
                std::cerr<<"Failed to run script : "<<*asci<<".\n";

        } else {
                number = result->Uint32Value();
                std::cout<<"Task result is good: "<<number<<std::endl;
        }
}

It doesn't work correctly. It crashes, when It should do more
things(wait). Can I solve this problem, with Locker?

Thank you for your help.

Bye d.

-- 
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users

Reply via email to