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();
}