Beispiel #1
0
void BookWrap::Setter(uint32_t index, Local<Value> value, const PropertyCallbackInfo<Value>& info) {
    Isolate* isolate = info.GetIsolate();
    HandleScope scope(isolate);
    
    BookWrap* bw = ObjectWrap::Unwrap<BookWrap>(info.This());
    Book*     b  = bw->m_book;

    if (value->IsArray()) {
        if (index < b->size()) {
            Local<v8::Array> arr = Local<v8::Array>::Cast(value);
            if (arr->Length() == 3) {
                const String::Utf8Value firstname(arr->Get(0)->ToString());
                const String::Utf8Value lastname(arr->Get(1)->ToString());
                const time_t birthday = time_t(0.001*(*arr->Get(2))->NumberValue());
                Person *p = (*b)[index];
                p->firstname(*firstname);
                p->lastname(*lastname);
                p->birthday(birthday);
            }
            else {
                isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "Three elements expected"))); 
                info.GetReturnValue().SetUndefined();
                return;               
            }
        }
        if (index == b->size()) {
            Local<v8::Array> arr = Local<v8::Array>::Cast(value);
            if (arr->Length() == 3) {
                const String::Utf8Value firstname(arr->Get(0)->ToString());
                const String::Utf8Value lastname(arr->Get(1)->ToString());
                const time_t birthday = time_t(0.001*(*arr->Get(2))->NumberValue());
                Person *p = new Person();
                p->firstname(*firstname);
                p->lastname(*lastname);
                p->birthday(birthday);
                b->add(p);
            }
            else {
                isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "Three elements expected")));
                info.GetReturnValue().SetUndefined();
                return;              
            }
        }
        else {
            isolate->ThrowException(Exception::RangeError(String::NewFromUtf8(isolate, "Invalid index")));
            info.GetReturnValue().SetUndefined();
            return;
        }
    }
    else {
        isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "Object expected")));
        info.GetReturnValue().SetUndefined();
        return;
    }
    info.GetReturnValue().SetUndefined();
}
Beispiel #2
0
int main(int argc, char* argv[]) {
    Book   b;

    Person *p1 = new Person();
    p1->firstname("Arthur");
    p1->lastname("Clarke");
    p1->birthday(-1642381200); // 1917-12-16
    b.add(p1);

    Person *p2 = new Person();
    p2->firstname("Peter");
    p2->lastname("Hamilton");
    p2->birthday(-310352400);  // 1960-03-02
    b.add(p2);

    Person *p3 = new Person();
    p3->firstname("Cory");
    p3->lastname("Doctorow");
    p3->birthday(48553200);    // 1971-07-17
    b.add(p3);

    Person *p4 = new Person();
    p4->firstname("Charlie");
    p4->lastname("Stross");
    p4->birthday(-164221200);  // 1964-10-18
    b.add(p4);

    for(size_t i=0; i<b.size(); ++i) {
        Person *p = b[i];
        cout << p->to_str() << endl;
    }

    cout << "Looking for Peter" << endl;
    Person *p5 = b.lookup("Peter");
    cout << p5->to_str() << endl;

    b.remove(1);
    cout << "After remove: " << b.size() << endl;
}
Beispiel #3
0
void BookWrap::Add(const v8::FunctionCallbackInfo<v8::Value>& args) {
    Isolate* isolate = args.GetIsolate();
    HandleScope scope(isolate);

    if (args.Length() == 1) {
        BookWrap* bw = ObjectWrap::Unwrap<BookWrap>(args.This());
        Book* b      = bw->m_book;
        PersonWrap* pw = ObjectWrap::Unwrap<PersonWrap>(args[0]->ToObject());
        b->add(pw->m_person);
    }
    else {
        isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "Object expected")));
    }
    args.GetReturnValue().SetUndefined();
}