Пример #1
0
result_t util_base::isEmpty(v8::Local<v8::Value> v, bool& retVal)
{
    if (v->IsNull()) {
        retVal = true;
        return 0;
    }

    if (v->IsString()) {
        retVal = v8::Local<v8::String>::Cast(v)->Length() == 0;
        return 0;
    }

    if (v->IsStringObject()) {
        retVal = v8::Local<v8::StringObject>::Cast(v)->ValueOf()->Length() == 0;
        return 0;
    }

    if (v->IsArray()) {
        retVal = v8::Local<v8::Array>::Cast(v)->Length() == 0;
        return 0;
    }

    if (v->IsObject()) {
        retVal = v->ToObject()->GetOwnPropertyNames()->Length() == 0;
        return 0;
    }

    retVal = true;

    return 0;
}
Пример #2
0
result_t util_base::map(v8::Local<v8::Value> list, v8::Local<v8::Function> iterator,
                        v8::Local<v8::Value> context, v8::Local<v8::Array> &retVal)
{
    Isolate* isolate = Isolate::current();
    v8::Local<v8::Array> arr = v8::Array::New(isolate->m_isolate);

    if (!list->IsObject())
    {
        retVal = arr;
        return 0;
    }

    v8::Local<v8::Value> args[3];
    args[2] = list;

    v8::Local<v8::Object> o = v8::Local<v8::Object>::Cast(list);
    v8::Local<v8::Value> v = o->Get(isolate->NewFromUtf8("length"));
    int32_t cnt = 0;

    if (IsEmpty(v))
    {
        v8::Local<v8::Array> keys = o->GetPropertyNames();
        int32_t len = keys->Length();
        int32_t i;

        for (i = 0; i < len; i ++)
        {
            args[1] = keys->Get(i);
            args[0] = o->Get(args[1]);

            v = iterator->Call(context, 3, args);
            if (v.IsEmpty())
                return CALL_E_JAVASCRIPT;

            arr->Set(cnt ++, v);
        }
    }
    else
    {
        int32_t len = v->Int32Value();

        int32_t i;

        for (i = 0; i < len; i ++)
        {
            args[1] = v8::Int32::New(isolate->m_isolate, i);
            args[0] = o->Get(args[1]);

            v = iterator->Call(context, 3, args);
            if (v.IsEmpty())
                return CALL_E_JAVASCRIPT;

            arr->Set(cnt ++, v);
        }
    }

    retVal = arr;

    return 0;
}
void V8ArrayBufferOrArrayBufferViewOrDictionary::toImpl(v8::Isolate* isolate, v8::Local<v8::Value> v8Value, ArrayBufferOrArrayBufferViewOrDictionary& impl, UnionTypeConversionMode conversionMode, ExceptionState& exceptionState)
{
    if (v8Value.IsEmpty())
        return;

    if (conversionMode == UnionTypeConversionMode::Nullable && isUndefinedOrNull(v8Value))
        return;

    if (v8Value->IsArrayBuffer()) {
        TestArrayBuffer* cppValue = V8ArrayBuffer::toImpl(v8::Local<v8::Object>::Cast(v8Value));
        impl.setArrayBuffer(cppValue);
        return;
    }

    if (v8Value->IsArrayBufferView()) {
        TestArrayBufferView* cppValue = V8ArrayBufferView::toImpl(v8::Local<v8::Object>::Cast(v8Value));
        impl.setArrayBufferView(cppValue);
        return;
    }

    if (isUndefinedOrNull(v8Value) || v8Value->IsObject()) {
        Dictionary cppValue = Dictionary(v8Value, isolate, exceptionState);
        if (exceptionState.hadException())
            return;
        impl.setDictionary(cppValue);
        return;
    }

    exceptionState.throwTypeError("The provided value is not of type '(ArrayBuffer or ArrayBufferView or Dictionary)'");
}
static PassRefPtr<WebKitFlags> getFlags(const v8::Local<v8::Value>& arg, ExceptionCode& ec)
{
    ec = 0;
    if (isUndefinedOrNull(arg) || !arg->IsObject())
        return 0;

    v8::Handle<v8::Object> object;
    {
        v8::TryCatch block;
        object = v8::Handle<v8::Object>::Cast(arg);
        if (block.HasCaught()) {
            ec = TYPE_MISMATCH_ERR;
            return 0;
        }
    }

    bool isCreate = extractBooleanValue(object, "create", ec);
    if (ec)
        return 0;
    bool isExclusive = extractBooleanValue(object, "exclusive", ec);
    if (ec)
        return 0;

    RefPtr<WebKitFlags> flags = WebKitFlags::create();
    flags->setCreate(isCreate);
    flags->setExclusive(isExclusive);

    return flags;
}
Пример #5
0
void Msg_Struct::build_buffer_struct(const Field_Info &field_info, Block_Buffer &buffer, Isolate* isolate, v8::Local<v8::Value> value) {
    Struct_Name_Map::iterator it = MSG_MANAGER->msg_struct_name_map().find(field_info.field_type);
    if(it == MSG_MANAGER->msg_struct_name_map().end()) {
        LOG_ERROR("Can not find the struct_name:%s", field_info.field_type.c_str());
        return;
    }

    if (!value->IsObject()) {
        LOG_ERROR("field_type:%s field_name:%s is not object, struct_name:%s", field_info.field_type.c_str(), field_info.field_name.c_str(), struct_name().c_str());
        return;
    }

    Local<Object> object = value->ToObject(isolate->GetCurrentContext()).ToLocalChecked();
    std::vector<Field_Info> field_vec = it->second->field_vec();
    for(std::vector<Field_Info>::const_iterator iter = field_vec.begin();
            iter != field_vec.end(); iter++) {
        Local<Value> element = object->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, iter->field_name.c_str(), NewStringType::kNormal).ToLocalChecked()).ToLocalChecked();
        if(iter->field_label == "arg") {
            build_buffer_arg(*iter, buffer, isolate, element);
        }
        else if(iter->field_label == "vector") {
            build_buffer_vector(*iter, buffer, isolate, element);
        }
        else if(iter->field_label == "map") {
            build_buffer_map(*iter, buffer, isolate, element);
        }
        else if(iter->field_label == "struct") {
            build_buffer_struct(*iter, buffer, isolate, element);
        }
    }
}
Пример #6
0
result_t util_base::values(v8::Local<v8::Value> v, v8::Local<v8::Array> &retVal)
{
    Isolate* isolate = Isolate::current();
    if (v->IsObject())
    {
        v8::Local<v8::Object> obj = v->ToObject();
        v8::Local<v8::Array> keys = obj->GetPropertyNames();
        v8::Local<v8::Array> arr = v8::Array::New(isolate->m_isolate);

        int32_t len = keys->Length();
        int32_t i, n = 0;

        for (i = 0; i < len; i ++)
        {
            v8::Local<v8::Value> key = keys->Get(i);
            arr->Set(n++, obj->Get(key));
        }

        retVal = arr;
    }
    else
        retVal = v8::Array::New(isolate->m_isolate);

    return 0;
}
Пример #7
0
result_t util_base::clone(v8::Local<v8::Value> v, v8::Local<v8::Value> &retVal)
{
    if (!v->IsProxy() && v->IsObject() && !object_base::getInstance(v))
    {
        Isolate* isolate = Isolate::current();

        if (v->IsFunction() || v->IsArgumentsObject() || v->IsSymbolObject())
            retVal = v;
        else if (v->IsDate())
            retVal = v8::Date::New(isolate->m_isolate, v->NumberValue());
        else if (v->IsBooleanObject())
            retVal = v8::BooleanObject::New(v->BooleanValue());
        else if (v->IsNumberObject())
            retVal = v8::NumberObject::New(isolate->m_isolate, v->NumberValue());
        else if (v->IsStringObject())
            retVal = v8::StringObject::New(v->ToString());
        else if (v->IsRegExp())
        {
            v8::Local<v8::RegExp> re = v8::Local<v8::RegExp>::Cast(v);
            retVal = v8::RegExp::New(re->GetSource(), re->GetFlags());
        }
        else
            retVal = v8::Local<v8::Object>::Cast(v)->Clone();
    }
    else
        retVal = v;

    return 0;
}
jobject TypeConverter::jsValueToJavaError(JNIEnv *env, v8::Local<v8::Value> jsValue, bool* isNew)
{
	if (jsValue->IsObject()) {
		v8::Handle<v8::Object> jsObject = jsValue->ToObject();

		// If it's a java object, we just return null for now.
		if (!JavaObject::isJavaObject(jsObject)) {

			Handle<String> stackString = String::New("stack"), messageString = String::New("message");
			if (jsObject->HasOwnProperty(stackString) || jsObject->HasOwnProperty(messageString)) {
				bool keyIsNew, valueIsNew;
				*isNew = true;
				v8::Local<v8::Value> jsObjectMessageProperty = jsObject->GetRealNamedProperty(messageString);
				v8::Local<v8::Value> jsObjectStackProperty = jsObject->GetRealNamedProperty(stackString);

				return env->NewObject(JNIUtil::krollExceptionClass, JNIUtil::krollExceptionInitMethod,
							TypeConverter::jsValueToJavaString(env, jsObjectMessageProperty), TypeConverter::jsValueToJavaString(env, jsObjectStackProperty));
			}
		}

	} else  {
		*isNew = true;
		return env->NewObject(JNIUtil::krollExceptionClass, JNIUtil::krollExceptionInitMethod,
			TypeConverter::jsValueToJavaString(env, jsValue), NULL);
	}

	if (!jsValue->IsNull() && !jsValue->IsUndefined()) {
		LOGW(TAG, "jsValueToJavaObject returning null.");
	}
	return NULL;
}
Пример #9
0
void V8TestInterfaceEventInit::toImpl(v8::Isolate* isolate, v8::Local<v8::Value> v8Value, TestInterfaceEventInit& impl, ExceptionState& exceptionState) {
  if (isUndefinedOrNull(v8Value)) {
    return;
  }
  if (!v8Value->IsObject()) {
    exceptionState.throwTypeError("cannot convert to dictionary.");
    return;
  }

  V8EventInit::toImpl(isolate, v8Value, impl, exceptionState);
  if (exceptionState.hadException())
    return;

  v8::TryCatch block(isolate);
  v8::Local<v8::Object> v8Object;
  if (!v8Call(v8Value->ToObject(isolate->GetCurrentContext()), v8Object, block)) {
    exceptionState.rethrowV8Exception(block.Exception());
    return;
  }
  v8::Local<v8::Value> stringMemberValue;
  if (!v8Object->Get(isolate->GetCurrentContext(), v8String(isolate, "stringMember")).ToLocal(&stringMemberValue)) {
    exceptionState.rethrowV8Exception(block.Exception());
    return;
  }
  if (stringMemberValue.IsEmpty() || stringMemberValue->IsUndefined()) {
    // Do nothing.
  } else {
    V8StringResource<> stringMember = stringMemberValue;
    if (!stringMember.prepare(exceptionState))
      return;
    impl.setStringMember(stringMember);
  }
}
Пример #10
0
void convertV8ObjectToNPVariant(v8::Local<v8::Value> object, NPObject* owner, NPVariant* result)
{
    VOID_TO_NPVARIANT(*result);

    // It is really the caller's responsibility to deal with the empty handle case because there could be different actions to
    // take in different contexts.
    ASSERT(!object.IsEmpty());

    if (object.IsEmpty())
        return;

    if (object->IsNumber())
        DOUBLE_TO_NPVARIANT(object->NumberValue(), *result);
    else if (object->IsBoolean())
        BOOLEAN_TO_NPVARIANT(object->BooleanValue(), *result);
    else if (object->IsNull())
        NULL_TO_NPVARIANT(*result);
    else if (object->IsUndefined())
        VOID_TO_NPVARIANT(*result);
    else if (object->IsString()) {
        v8::Handle<v8::String> str = object->ToString();
        int length = str->Utf8Length() + 1;
        char* utf8Chars = reinterpret_cast<char*>(malloc(length));
        str->WriteUtf8(utf8Chars, length, 0, v8::String::HINT_MANY_WRITES_EXPECTED);
        STRINGN_TO_NPVARIANT(utf8Chars, length-1, *result);
    } else if (object->IsObject()) {
        DOMWindow* window = V8Proxy::retrieveWindow(V8Proxy::currentContext());
        NPObject* npobject = npCreateV8ScriptObject(0, v8::Handle<v8::Object>::Cast(object), window);
        if (npobject)
            _NPN_RegisterObject(npobject, owner);
        OBJECT_TO_NPVARIANT(npobject, *result);
    }
}
void V8TestPermissiveDictionary::toImpl(v8::Isolate* isolate, v8::Local<v8::Value> v8Value, TestPermissiveDictionary& impl, ExceptionState& exceptionState)
{
    if (isUndefinedOrNull(v8Value))
        return;
    if (!v8Value->IsObject()) {
        // Do nothing.
        return;
    }

    v8::TryCatch block;
    v8::Local<v8::Object> v8Object;
    if (!v8Call(v8Value->ToObject(isolate->GetCurrentContext()), v8Object, block)) {
        exceptionState.rethrowV8Exception(block.Exception());
        return;
    }
    {
        v8::Local<v8::Value> booleanMemberValue;
        if (!v8Object->Get(isolate->GetCurrentContext(), v8String(isolate, "booleanMember")).ToLocal(&booleanMemberValue)) {
            exceptionState.rethrowV8Exception(block.Exception());
            return;
        }
        if (booleanMemberValue.IsEmpty() || booleanMemberValue->IsUndefined()) {
            // Do nothing.
        } else {
            bool booleanMember = toBoolean(isolate, booleanMemberValue, exceptionState);
            if (exceptionState.hadException())
                return;
            impl.setBooleanMember(booleanMember);
        }
    }

}
Пример #12
0
Dictionary::Dictionary(v8::Isolate* isolate,
                       v8::Local<v8::Value> dictionaryObject,
                       ExceptionState& exceptionState)
    : m_isolate(isolate) {
  DCHECK(isolate);

  // https://heycam.github.io/webidl/#es-dictionary
  // Type of an ECMAScript value must be Undefined, Null or Object.
  if (dictionaryObject.IsEmpty() || dictionaryObject->IsUndefined()) {
    m_valueType = ValueType::Undefined;
    return;
  }
  if (dictionaryObject->IsNull()) {
    m_valueType = ValueType::Null;
    return;
  }
  if (dictionaryObject->IsObject()) {
    m_valueType = ValueType::Object;
    m_dictionaryObject = dictionaryObject.As<v8::Object>();
    return;
  }

  exceptionState.throwTypeError(
      "The dictionary provided is neither undefined, null nor an Object.");
}
Пример #13
0
void convertV8ObjectToNPVariant(v8::Local<v8::Value> object, NPObject* owner, NPVariant* result)
{
    VOID_TO_NPVARIANT(*result);

    // It is really the caller's responsibility to deal with the empty handle case because there could be different actions to
    // take in different contexts.
    ASSERT(!object.IsEmpty());

    if (object.IsEmpty())
        return;

    if (object->IsInt32())
        INT32_TO_NPVARIANT(object->NumberValue(), *result);
    else if (object->IsNumber())
        DOUBLE_TO_NPVARIANT(object->NumberValue(), *result);
    else if (object->IsBoolean())
        BOOLEAN_TO_NPVARIANT(object->BooleanValue(), *result);
    else if (object->IsNull())
        NULL_TO_NPVARIANT(*result);
    else if (object->IsUndefined())
        VOID_TO_NPVARIANT(*result);
    else if (object->IsString()) {
        v8::String::Utf8Value utf8(object);
        char* utf8_chars = strdup(*utf8);
        STRINGN_TO_NPVARIANT(utf8_chars, utf8.length(), *result);
    } else if (object->IsObject()) {
        WebCore::DOMWindow* window = WebCore::V8Proxy::retrieveWindow(WebCore::V8Proxy::currentContext());
        NPObject* npobject = npCreateV8ScriptObject(0, v8::Handle<v8::Object>::Cast(object), window);
        if (npobject)
            _NPN_RegisterObject(npobject, owner);
        OBJECT_TO_NPVARIANT(npobject, *result);
    }
}
v8::Local<v8::Object> Convert::getJsObject(v8::Local<v8::Value> js)
{
    if (!js->IsObject())
    {
        throw std::runtime_error("object");
    }

    return js->ToObject();
}
Пример #15
0
jobject v8ToJava(JNIEnv* env, v8::Local<v8::Value> arg) {
  if(arg->IsNull() || arg->IsUndefined()) {
    return NULL;
  }

  if(arg->IsArray()) {
    v8::Local<v8::Array> array = v8::Array::Cast(*arg);
    uint32_t arraySize = array->Length();
    jclass objectClazz = env->FindClass("java/lang/Object");
    jobjectArray result = env->NewObjectArray(arraySize, objectClazz, NULL);
    for(uint32_t i=0; i<arraySize; i++) {
      jobject val = v8ToJava(env, array->Get(i));
      env->SetObjectArrayElement(result, i, val);
    }
    return result;
  }

  if(arg->IsString()) {
    v8::String::AsciiValue val(arg->ToString());
    return env->NewStringUTF(*val);
  }

  if(arg->IsInt32() || arg->IsUint32()) {
    jint val = arg->ToInt32()->Value();
    jclass clazz = env->FindClass("java/lang/Integer");
    jmethodID constructor = env->GetMethodID(clazz, "<init>", "(I)V");
    return env->NewObject(clazz, constructor, val);
  }

  if(arg->IsNumber()) {
    jdouble val = arg->ToNumber()->Value();
    jclass clazz = env->FindClass("java/lang/Double");
    jmethodID constructor = env->GetMethodID(clazz, "<init>", "(D)V");
    return env->NewObject(clazz, constructor, val);
  }

  if(arg->IsBoolean()) {
    jboolean val = arg->ToBoolean()->Value();
    jclass clazz = env->FindClass("java/lang/Boolean");
    jmethodID constructor = env->GetMethodID(clazz, "<init>", "(Z)V");
    return env->NewObject(clazz, constructor, val);
  }

  if(arg->IsObject()) {
    v8::Local<v8::Object> obj = v8::Object::Cast(*arg);
    v8::String::AsciiValue constructorName(obj->GetConstructorName());
    if(strcmp(*constructorName, "JavaObject") == 0) {
      JavaObject* javaObject = node::ObjectWrap::Unwrap<JavaObject>(obj);
      return javaObject->getObject();
    }
  }

  // TODO: handle other arg types
  v8::String::AsciiValue typeStr(arg);
  printf("Unhandled type: %s\n", *typeStr);
  return NULL;
}
Пример #16
0
v8::Local<v8::Object> V8PerIsolateData::findInstanceInPrototypeChain(const WrapperTypeInfo* info, v8::Local<v8::Value> value, DOMTemplateMap& domTemplateMap)
{
    if (value.IsEmpty() || !value->IsObject())
        return v8::Local<v8::Object>();
    DOMTemplateMap::iterator result = domTemplateMap.find(info);
    if (result == domTemplateMap.end())
        return v8::Local<v8::Object>();
    v8::Local<v8::FunctionTemplate> templ = result->value.Get(isolate());
    return v8::Local<v8::Object>::Cast(value)->FindInstanceInPrototypeChain(templ);
}
V8NodeFilterCondition::V8NodeFilterCondition(v8::Local<v8::Value> filter, v8::Local<v8::Object> owner, ScriptState* scriptState)
    : m_scriptState(scriptState)
{
    // ..acceptNode(..) will only dispatch m_filter if m_filter->IsObject().
    // We'll make sure m_filter is either usable by acceptNode or empty.
    // (See the fast/dom/node-filter-gc test for a case where 'empty' happens.)
    if (!filter.IsEmpty() && filter->IsObject()) {
        V8HiddenValue::setHiddenValue(scriptState->isolate(), owner, V8HiddenValue::condition(scriptState->isolate()), filter);
        m_filter.set(scriptState->isolate(), filter);
        m_filter.setWeak(this, &setWeakCallback);
    }
}
Пример #18
0
result_t util_base::has(v8::Local<v8::Value> v, const char *key, bool &retVal)
{
    if (v->IsUndefined() || v->IsNull())
    {
        retVal = false;
        return 0;
    }

    if (!v->IsObject())
        return CHECK_ERROR(CALL_E_TYPEMISMATCH);

    v8::Local<v8::Object> obj = v->ToObject();
    retVal = obj->HasOwnProperty(Isolate::current()->NewFromUtf8(key));
    return 0;
}
Пример #19
0
void V8Element::scrollTopAttributeSetterCustom(v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<void>& info)
{
    ExceptionState exceptionState(ExceptionState::SetterContext, "scrollTop", "Element", info.Holder(), info.GetIsolate());
    Element* imp = V8Element::toNative(info.Holder());

    if (RuntimeEnabledFeatures::cssomSmoothScrollEnabled() && value->IsObject()) {
        V8TRYCATCH_VOID(Dictionary, scrollOptionsVertical, Dictionary(value, info.GetIsolate()));
        imp->setScrollTop(scrollOptionsVertical, exceptionState);
        exceptionState.throwIfNeeded();
        return;
    }

    V8TRYCATCH_EXCEPTION_VOID(int, position, toInt32(value, exceptionState), exceptionState);
    imp->setScrollTop(position);
}
Пример #20
0
    Accumulate(Callback *data
      , Callback *complete
      , Callback *error_callback, 
      v8::Local<v8::Object> & options) : StreamingWorker(data, complete, error_callback){

        sum = 0;
        filter = "";
        if (options->IsObject() ) {
          v8::Local<v8::Value> filter_ = options->Get(New<v8::String>("filter").ToLocalChecked());
          if ( filter_->IsString() ) {
            v8::String::Utf8Value s(filter_);
            filter = *s;
          }
        }
    }
Пример #21
0
bool V8DOMWrapper::isWrapper(v8::Isolate* isolate, v8::Local<v8::Value> value)
{
    if (value.IsEmpty() || !value->IsObject())
        return false;
    v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(value);

    if (object->InternalFieldCount() < v8DefaultWrapperInternalFieldCount)
        return false;

    const WrapperTypeInfo* untrustedWrapperTypeInfo = toWrapperTypeInfo(object);
    V8PerIsolateData* perIsolateData = V8PerIsolateData::from(isolate);
    if (!(untrustedWrapperTypeInfo && perIsolateData))
        return false;
    return perIsolateData->hasInstance(untrustedWrapperTypeInfo, object);
}
Пример #22
0
bool V8DOMWrapper::hasInternalFieldsSet(v8::Local<v8::Value> value)
{
    if (value.IsEmpty() || !value->IsObject())
        return false;
    v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(value);

    if (object->InternalFieldCount() < v8DefaultWrapperInternalFieldCount)
        return false;

    const ScriptWrappable* untrustedScriptWrappable = toScriptWrappable(object);
    const WrapperTypeInfo* untrustedWrapperTypeInfo = toWrapperTypeInfo(object);
    return untrustedScriptWrappable
        && untrustedWrapperTypeInfo
        && untrustedWrapperTypeInfo->ginEmbedder == gin::kEmbedderBlink;
}
SassImportList CustomImporterBridge::post_process_return_value(v8::Local<v8::Value> returned_value) const {
  SassImportList imports = 0;
  Nan::HandleScope scope;

  if (returned_value->IsArray()) {
    v8::Local<v8::Array> array = returned_value.As<v8::Array>();

    imports = sass_make_import_list(array->Length());

    for (size_t i = 0; i < array->Length(); ++i) {
      v8::Local<v8::Value> value = Nan::Get(array, static_cast<uint32_t>(i)).ToLocalChecked();

      if (!value->IsObject()) {
        auto entry = sass_make_import_entry(0, 0, 0);
        sass_import_set_error(entry, "returned array must only contain object literals", -1, -1);
        continue;
      }

      v8::Local<v8::Object> object = value.As<v8::Object>();

      if (value->IsNativeError()) {
        char* message = create_string(Nan::Get(object, Nan::New<v8::String>("message").ToLocalChecked()));

        imports[i] = sass_make_import_entry(0, 0, 0);

        sass_import_set_error(imports[i], message, -1, -1);
      }
      else {
        imports[i] = get_importer_entry(object);
      }
    }
  }
  else if (returned_value->IsNativeError()) {
    imports = sass_make_import_list(1);
    v8::Local<v8::Object> object = returned_value.As<v8::Object>();
    char* message = create_string(Nan::Get(object, Nan::New<v8::String>("message").ToLocalChecked()));

    imports[0] = sass_make_import_entry(0, 0, 0);

    sass_import_set_error(imports[0], message, -1, -1);
  }
  else if (returned_value->IsObject()) {
    imports = sass_make_import_list(1);
    imports[0] = get_importer_entry(returned_value.As<v8::Object>());
  }

  return imports;
}
Пример #24
0
bool extractTransferables(v8::Local<v8::Value> value, MessagePortArray& ports, ArrayBufferArray& arrayBuffers)
{
    if (isUndefinedOrNull(value)) {
        ports.resize(0);
        arrayBuffers.resize(0);
        return true;
    }

    if (!value->IsObject()) {
        throwError("TransferArray argument must be an object");
        return false;
    }
    uint32_t length = 0;
    v8::Local<v8::Object> transferrables = v8::Local<v8::Object>::Cast(value);

    if (value->IsArray()) {
        v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(value);
        length = array->Length();
    } else {
        // Sequence-type object - get the length attribute
        v8::Local<v8::Value> sequenceLength = transferrables->Get(v8::String::New("length"));
        if (!sequenceLength->IsNumber()) {
            throwError("TransferArray argument has no length attribute");
            return false;
        }
        length = sequenceLength->Uint32Value();
    }

    // Validate the passed array of transferrables.
    for (unsigned int i = 0; i < length; ++i) {
        v8::Local<v8::Value> transferrable = transferrables->Get(i);
        // Validation of non-null objects, per HTML5 spec 10.3.3.
        if (isUndefinedOrNull(transferrable)) {
            throwError(DATA_CLONE_ERR);
            return false;
        }
        // Validation of Objects implementing an interface, per WebIDL spec 4.1.15.
        if (V8MessagePort::HasInstance(transferrable))
            ports.append(V8MessagePort::toNative(v8::Handle<v8::Object>::Cast(transferrable)));
        else if (V8ArrayBuffer::HasInstance(transferrable))
            arrayBuffers.append(V8ArrayBuffer::toNative(v8::Handle<v8::Object>::Cast(transferrable)));
        else {
            throwError("TransferArray argument must contain only Transferables");
            return false;
        }
    }
    return true;
}
v8::Local<v8::Object> Convert::getJsObjectOrNull(v8::Local<v8::Value> js)
{
    if (js->IsNull())
    {
        Nan::EscapableHandleScope scope;
        v8::Local<v8::Object> newobj = Nan::New<v8::Object>();
        Utility::Set(newobj, "special_hack_null_object", true);
        return scope.Escape(newobj);
    }
    else if (js->IsObject())
    {
        return Convert::getJsObject(js);
    }

    throw std::runtime_error("object or null");
}
Пример #26
0
result_t SandBox::require(const char *id, v8::Local<v8::Value> &retVal)
{
    std::string fname = resolvePath(id);
    std::map<std::string, VariantEx >::iterator it;

    // remove .js ext name if exists
    if (fname.length() > 3 && !qstrcmp(&fname[fname.length() - 3], ".js"))
        fname.resize(fname.length() - 3);

    it = m_mods.find(fname);

    if (it != m_mods.end())
    {
        retVal = it->second;
        return 1;
    }

    if (!m_require.IsEmpty())
    {
        v8::Local<v8::Value> arg = v8::String::NewFromUtf8(isolate, fname.c_str());
        retVal = v8::Local<v8::Function>::New(isolate, m_require)->Call(wrap(), 1, &arg);
        if (retVal.IsEmpty())
            return CALL_E_JAVASCRIPT;

        if (!IsEmpty(retVal))
        {
            if (retVal->IsObject() && !object_base::getInstance(retVal))
                retVal = retVal->ToObject()->Clone();
            InstallModule(fname, retVal);

            return 0;
        }
    }

    // append .js ext name
    fname += ".js";

    result_t hr;
    const char *pname = fname.c_str();

    std::string buf;
    hr = fs_base::ac_readFile(pname, buf);
    if (hr < 0)
        return hr;

    return addScript(pname, buf.c_str(), retVal);
}
Пример #27
0
result_t util_base::pick(v8::Local<v8::Value> v,
                         const v8::FunctionCallbackInfo<v8::Value> &args,
                         v8::Local<v8::Object> &retVal)
{
    Isolate* isolate = Isolate::current();
    if (v->IsUndefined() || v->IsNull())
    {
        retVal = v8::Object::New(isolate->m_isolate);
        return 0;
    }

    if (!v->IsObject())
        return CHECK_ERROR(CALL_E_TYPEMISMATCH);

    v8::Local<v8::Object> obj = v->ToObject();
    v8::Local<v8::Object> obj1 = v8::Object::New(isolate->m_isolate);
    int32_t argc = args.Length();
    int32_t i, j;

    for (i = 1; i < argc; i ++)
    {
        if (args[i]->IsArray())
        {
            v8::Local<v8::Array> arr = v8::Local<v8::Array>::Cast(args[i]);
            int32_t len = arr->Length();

            for (j = 0; j < len; j ++)
            {
                v8::Local<v8::Value> k = arr->Get(j);

                if (obj->Has(k))
                    obj1->Set(k, obj->Get(k));
            }
        }
        else
        {
            v8::Local<v8::Value> k = args[i];

            if (obj->Has(k))
                obj1->Set(k, obj->Get(k));
        }
    }

    retVal = obj1;

    return 0;
}
Пример #28
0
result_t util_base::flatten(v8::Local<v8::Value> list, bool shallow,
                            v8::Local<v8::Array> &retVal)
{
    if (!list->IsObject())
        return CHECK_ERROR(CALL_E_TYPEMISMATCH);

    bool bNext = true;

    Isolate* isolate = Isolate::current();

    if (retVal.IsEmpty())
        retVal = v8::Array::New(isolate->m_isolate);
    else if (shallow)
        bNext = false;

    v8::Local<v8::Object> o = v8::Local<v8::Object>::Cast(list);
    v8::Local<v8::Value> v = o->Get(isolate->NewFromUtf8("length"));
    if (IsEmpty(v))
        return CHECK_ERROR(CALL_E_TYPEMISMATCH);

    int32_t len = v->Int32Value();
    int32_t cnt = retVal->Length();
    int32_t i;

    for (i = 0; i < len; i ++)
    {
        v = o->Get(i);
        if (bNext && v->IsObject())
        {
            v8::Local<v8::Object> o1 = v8::Local<v8::Object>::Cast(v);
            v = o->Get(isolate->NewFromUtf8("length"));
            if (IsEmpty(v))
                retVal->Set(cnt ++, o->Get(i));
            else
            {
                flatten(o1, shallow, retVal);
                cnt = retVal->Length();
            }
        }
        else
            retVal->Set(cnt ++, o->Get(i));
    }

    return 0;
}
bool getMessagePortArray(v8::Local<v8::Value> value, MessagePortArray& portArray)
{
    if (isUndefinedOrNull(value)) {
        portArray.resize(0);
        return true;
    }

    if (!value->IsObject()) {
        throwError("MessagePortArray argument must be an object");
        return false;
    }
    uint32_t length = 0;
    v8::Local<v8::Object> ports = v8::Local<v8::Object>::Cast(value);

    if (value->IsArray()) {
        v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(value);
        length = array->Length();
    } else {
        // Sequence-type object - get the length attribute
        v8::Local<v8::Value> sequenceLength = ports->Get(v8::String::New("length"));
        if (!sequenceLength->IsNumber()) {
            throwError("MessagePortArray argument has no length attribute");
            return false;
        }
        length = sequenceLength->Uint32Value();
    }
    portArray.resize(length);

    for (unsigned int i = 0; i < length; ++i) {
        v8::Local<v8::Value> port = ports->Get(v8::Integer::New(i));
        // Validation of non-null objects, per HTML5 spec 8.3.3.
        if (isUndefinedOrNull(port)) {
            throwError(INVALID_STATE_ERR);
            return false;
        }
        // Validation of Objects implementing an interface, per WebIDL spec 4.1.15.
        if (!V8MessagePort::HasInstance(port)) {
            throwError("MessagePortArray argument must contain only MessagePorts");
            return false;
        }
        portArray[i] = V8DOMWrapper::convertToNativeObject<MessagePort>(V8ClassIndex::MESSAGEPORT, v8::Handle<v8::Object>::Cast(port));
    }
    return true;
}
Пример #30
0
// static
CJS_Value::Type CJS_Value::GetValueType(v8::Local<v8::Value> value) {
  if (value.IsEmpty())
    return VT_unknown;
  if (value->IsString())
    return VT_string;
  if (value->IsNumber())
    return VT_number;
  if (value->IsBoolean())
    return VT_boolean;
  if (value->IsDate())
    return VT_date;
  if (value->IsObject())
    return VT_object;
  if (value->IsNull())
    return VT_null;
  if (value->IsUndefined())
    return VT_undefined;
  return VT_unknown;
}