Handle<Value> AddOne(const Arguments& args) { do_check_eq(args.Length(), 1); Local<Value> v = args[0]; do_check_true(v->IsNumber()); Local<Number> n = v->ToNumber(); return Number::New(n->Value() + 1.0); }
void test_BasicCall() { HandleScope handle_scope; Handle<ObjectTemplate> templ = ObjectTemplate::New(); Handle<FunctionTemplate> fnT = v8::FunctionTemplate::New(AddOne); templ->Set("AddOne", fnT); Persistent<Context> context = Context::New(NULL, templ); Context::Scope context_scope(context); Local<Value> addone = context->Global()->Get(String::New("AddOne")); do_check_true(!addone.IsEmpty()); do_check_true(!addone->IsUndefined()); do_check_true(addone->IsObject()); do_check_true(addone->IsFunction()); Local<Function> fn = addone.As<Function>(); do_check_eq(fn, fnT->GetFunction()); Local<Number> n = Number::New(0.5); Handle<Value> args[] = { n }; Local<Value> v = fn->Call(context->Global(), 1, args); do_check_true(!v.IsEmpty()); do_check_true(v->IsNumber()); Local<Number> n2 = v->ToNumber(); do_check_eq(n2->Value(), 1.5); context.Dispose(); }
void vectorTemplate::setVectorY(Local<String> property, Local<Value> value, const AccessorInfo &info){ Local<External> data = Local<External>::Cast(info.Data()); vector<double> *vect = reinterpret_cast<vector<double> *>(data->Value()); if(value->IsNumber()){ vect->y = value->ToNumber()->Value(); } }
bool JSWrapper::execute( const char *scr, JSWrapperData *data, const char *fileName ) { HandleScope handle_scope( m_isolate ); Local<String> source = String::NewFromUtf8( m_isolate, scr, NewStringType::kNormal ).ToLocalChecked(); ScriptOrigin origin( v8::String::NewFromUtf8( m_isolate, fileName ? fileName : "Unknown" ) ); MaybeLocal<Script> maybeScript = Script::Compile( m_context, source, &origin ); bool success=false; if ( !maybeScript.IsEmpty() ) { Local<Script> script = maybeScript.ToLocalChecked(); MaybeLocal<Value> maybeResult = script->Run(m_context); if ( !maybeResult.IsEmpty() ) { Local<Value> result = maybeResult.ToLocalChecked(); if ( data ) { if ( result->IsNumber() ) data->setNumber( result->ToNumber()->Value() ); else if ( result->IsString() ) { String::Utf8Value utf8( result ); data->setString( *utf8 ); } else if ( result->IsBoolean() ) data->setBoolean( result->ToBoolean()->Value() ); else if ( result->IsObject() ) data->setObject( new JSWrapperObject( m_isolate, result->ToObject() ) ); else if ( result->IsNull() ) data->setNull(); else data->setUndefined(); } success=true; } } return success; }
static Local<Value> convertType(TiAppPropertiesObject::PropertyType type, Local<Value> value) { switch (type) { case TiAppPropertiesObject::BoolProperty: return value->ToBoolean(); case TiAppPropertiesObject::DoubleProperty: case TiAppPropertiesObject::IntProperty: return value->ToNumber(); case TiAppPropertiesObject::ObjectProperty: return value->ToObject(); case TiAppPropertiesObject::ListProperty: return value; case TiAppPropertiesObject::StringProperty: return value->ToString(); default: return Local<Value>(); } }
void ExecuteBaton::GetVectorParam(ExecuteBaton* baton, arrayParam_t* arrParam, Local<Array> arr) { // In case the array is empty just initialize the fields as we would need something in Connection::SetValuesOnStatement if (arr->Length() < 1) { arrParam->value = new int[0]; arrParam->collectionLength = 0; arrParam->elementsSize = 0; arrParam->elementLength = new ub2[0]; arrParam->elementsType = oracle::occi::OCCIINT; return; } // Next we create the array buffer that will be used later as the value for the param (in Connection::SetValuesOnStatement) // The array type will be derived from the type of the first element. Local<Value> val = arr->Get(0); // String array if (val->IsString()) { arrParam->elementsType = oracle::occi::OCCI_SQLT_STR; // Find the longest string, this is necessary in order to create a buffer later. int longestString = 0; for(unsigned int i = 0; i < arr->Length(); i++) { Local<Value> currVal = arr->Get(i); if (currVal->ToString()->Utf8Length() > longestString) longestString = currVal->ToString()->Utf8Length(); } // Add 1 for '\0' ++longestString; // Create a long char* that will hold the entire array, it is important to create a FIXED SIZE array, // meaning all strings have the same allocated length. char* strArr = new char[arr->Length() * longestString]; arrParam->elementLength = new ub2[arr->Length()]; // loop thru the arr and copy the strings into the strArr int bytesWritten = 0; for(unsigned int i = 0; i < arr->Length(); i++) { Local<Value> currVal = arr->Get(i); if(!currVal->IsString()) { std::ostringstream message; message << "Input array has object with invalid type at index " << i << ", all object must be of type 'string' which is the type of the first element"; baton->error = new std::string(message.str()); return; } String::Utf8Value utfStr(currVal); // Copy this string onto the strArr (we put \0 in the beginning as this is what strcat expects). strArr[bytesWritten] = '\0'; strncat(strArr + bytesWritten, *utfStr, longestString); bytesWritten += longestString; // Set the length of this element, add +1 for the '\0' arrParam->elementLength[i] = utfStr.length() + 1; } arrParam->value = strArr; arrParam->collectionLength = arr->Length(); arrParam->elementsSize = longestString; } // Integer array. else if (val->IsNumber()) { arrParam->elementsType = oracle::occi::OCCI_SQLT_NUM; // Allocate memory for the numbers array, Number in Oracle is 21 bytes unsigned char* numArr = new unsigned char[arr->Length() * 21]; arrParam->elementLength = new ub2[arr->Length()]; for(unsigned int i = 0; i < arr->Length(); i++) { Local<Value> currVal = arr->Get(i); if(!currVal->IsNumber()) { std::ostringstream message; message << "Input array has object with invalid type at index " << i << ", all object must be of type 'number' which is the type of the first element"; baton->error = new std::string(message.str()); return; } // JS numbers can exceed oracle numbers, make sure this is not the case. double d = currVal->ToNumber()->Value(); if (d > 9.99999999999999999999999999999999999999*std::pow(double(10), double(125)) || d < -9.99999999999999999999999999999999999999*std::pow(double(10), double(125))) { std::ostringstream message; message << "Input array has number that is out of the range of Oracle numbers, check the number at index " << i; baton->error = new std::string(message.str()); return; } // Convert the JS number into Oracle Number and get its bytes representation oracle::occi::Number n = d; oracle::occi::Bytes b = n.toBytes(); arrParam->elementLength[i] = b.length (); b.getBytes(&numArr[i*21], b.length()); } arrParam->value = numArr; arrParam->collectionLength = arr->Length(); arrParam->elementsSize = 21; } // Unsupported type else { baton->error = new std::string("The type of the first element in the input array is not supported"); } }