Changeset 16102


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

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/scripts/doc/Thread.js

    r16053 r16102  
    3535 *    A function which is executed aftr the initial timeout.
    3636 *
     37 * @param {Object} [_this]
     38 *    An object which will be the reference for 'this' in the function call.
     39 *    If none is given, the function itself will be the 'this' object.
     40 *
    3741 * @throws
    3842 *    <li> If number or type of arguments is wrong
     
    4347 *    handle.kill();
    4448 */
    45 function Thread(timeout, function)
     49function Thread(timeout, function, _this)
    4650{
    4751    /**
  • trunk/FACT++/scripts/doc/v8.js

    r15074 r16102  
    3131 * main purpose of a loop is, e.g., to wait for something to happen.
    3232 *
    33  * @param {Integer} [milliseconds]
     33 * @param {Integer} milliseconds
    3434 *     Number of millliseconds until the timeout. Note that even 0
    3535 *     will execute the function at least once. If the timeout
     
    3737 *     be returned in case of a timeout.
    3838 *
    39  * @param {Function} [func]
     39 * @param {Function} func
    4040 *     A function. The function defines when the conditional to end
    4141 *     the timeout will be fullfilled. As soon as the function returns
     
    4343 *     timeout is stopped and its return value is returned.
    4444 *
     45 * @param {Object} [_this]
     46 *    An object which will be the reference for 'this' in the function call.
     47 *    If none is given, the function itself will be the 'this' object.
     48 *
    4549 * @param [. . .]
    46  *     Any further argument will be passed to the function.
     50 *     Any further argument will be passed as argument to the function.
    4751 *
    4852 * @returns
  • 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.