Changeset 16102 for trunk/FACT++/src


Ignore:
Timestamp:
05/24/13 13:36:21 (11 years ago)
Author:
tbretz
Message:
Added a user supplied 'this' argument to v8.timeout and the Thread class.
Location:
trunk/FACT++/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/InterpreterV8.cc

    r16095 r16102  
    141141        return ThrowException(String::New("Argument 1 not a function."));
    142142
     143    if (args.Length()>2 && !args[2]->IsObject())
     144        return ThrowException(String::New("Argument 2 not an object."));
     145
    143146    const int32_t timeout = args[0]->IsNull() ? 0 : args[0]->Int32Value();
    144147    const bool    null    = args[0]->IsNull();
     
    148151    Handle<Function> func = Handle<Function>::Cast(args[1]);
    149152
    150     Handle<Value> argv[args.Length()-2];
    151     for (int i=0; i<args.Length()-2; i++)
    152         argv[i] = args[i+2];
     153    const int nn = args.Length()==2 ? 0 : args.Length()-3;
     154
     155    Handle<Value> argv[nn];
     156    for (int i=0; i<nn; i++)
     157        argv[i] = args[i+3];
    153158
    154159    Time t;
    155160    while (1)
    156161    {
    157         const Handle<Value> rc = func->Call(func, args.Length()-2, argv);
     162        const Handle<Value> rc = args.Length()<3 ? func->Call(func, nn, argv) : func->Call(args[2]->ToObject(), nn, argv);
    158163        if (rc.IsEmpty())
    159164            return Undefined();
     
    179184}
    180185
    181 void InterpreterV8::Thread(int &id, Persistent<Function> func, uint32_t ms)
     186void InterpreterV8::Thread(int &id, Persistent<Object> _this, Persistent<Function> func, uint32_t ms)
    182187{
    183188    const Locker lock;
     
    206211    const bool rc = ms==0 || !ExecuteInternal("v8.sleep("+to_string(ms)+");").IsEmpty();
    207212    if (rc)
    208         func->Call(func, 0, NULL);
     213    {
     214        if (_this.IsEmpty())
     215            func->Call(func, 0, NULL);
     216        else
     217            func->Call(_this, 0, NULL);
     218    }
    209219
    210220    func.Dispose();
     221    _this.Dispose();
     222
    211223    fThreadIds.erase(id_local);
    212224
     
    220232        return ThrowException(String::New("Thread must be called as constructor."));
    221233
    222     if (args.Length()!=2)
    223         return ThrowException(String::New("Number of arguments must be two."));
     234    if (args.Length()!=2 && args.Length()!=3)
     235        return ThrowException(String::New("Number of arguments must be two or three."));
    224236
    225237    if (!args[0]->IsUint32())
     
    229241        return ThrowException(String::New("Argument 1 not a function."));
    230242
     243    if (args.Length()==3 && !args[2]->IsObject())
     244        return ThrowException(String::New("Argument 2 not an object."));
     245
    231246    //if (!args.IsConstructCall())
    232247    //    return Constructor(args);
     
    236251    Handle<Function> handle = Handle<Function>::Cast(args[1]);
    237252
    238     Persistent<Function> func = Persistent<Function>::New(handle);
     253    Persistent<Function> func =  Persistent<Function>::New(handle);
     254    Persistent<Object> _this;
     255    if (args.Length()==3)
     256        _this = Persistent<Object>::New(args[2]->ToObject());
    239257
    240258    const uint32_t ms = args[0]->Uint32Value();
    241259
    242260    int id=-2;
    243     fThreads.push_back(thread(bind(&InterpreterV8::Thread, this, ref(id), func, ms)));
     261    fThreads.push_back(thread(bind(&InterpreterV8::Thread, this, ref(id), _this, func, ms)));
    244262    {
    245263        // Allow the thread to lock, so we can get the thread id.
  • trunk/FACT++/src/InterpreterV8.h

    r16071 r16102  
    6565    v8::Handle<v8::Value> ExecuteInternal(const std::string &code);
    6666
    67     void Thread(int &id, v8::Persistent<v8::Function> func, uint32_t ms);
     67    void Thread(int &id, v8::Persistent<v8::Object> _this, v8::Persistent<v8::Function> func, uint32_t ms);
    6868
    6969    std::vector<std::string> ValueToArray(const v8::Handle<v8::Value> &val, bool only=true);
Note: See TracChangeset for help on using the changeset viewer.