void acceptValue(JSValuePtr val) { switch (val->getType()) { case JSValue::String: std::cout << "Received String:" << val->asString() << std::endl; break; case JSValue::Boolean: std::cout << "Received Bool:" << val->asBool() << std::endl; break; case JSValue::Number: std::cout << "Received Number:" << val->asDouble() << std::endl; break; case JSValue::Object: std::cout << "Received Object." << std::endl; break; case JSValue::Array: std::cout << "Received Array." << std::endl; break; case JSValue::Undefined: std::cout << "Received undefined." << std::endl; break; case JSValue::Null: std::cout << "Received null." << std::endl; break; default: std::cout << "Received unknown jsvalue type." << std::endl; break; } }
UString JSObject::toString(ExecState* exec) const { JSValuePtr primitive = toPrimitive(exec, PreferString); if (exec->hadException()) return ""; return primitive->toString(exec); }
UString JSImmediate::toString(JSValuePtr v) { ASSERT(isImmediate(v)); if (isIntegerNumber(v)) return UString::from(getTruncatedInt32(v)); #if USE(ALTERNATE_JSIMMEDIATE) if (isNumber(v)) { ASSERT(isDoubleNumber(v)); double value = doubleValue(v); if (value == 0.0) // +0.0 or -0.0 return "0"; return UString::from(value); } #else ASSERT(!isNumber(v)); #endif if (jsBoolean(false) == v) return "false"; if (jsBoolean(true) == v) return "true"; if (v.isNull()) return "null"; ASSERT(v.isUndefined()); return "undefined"; }
double JSObject::toNumber(ExecState* exec) const { JSValuePtr primitive = toPrimitive(exec, PreferNumber); if (exec->hadException()) // should be picked up soon in Nodes.cpp return 0.0; return primitive->toNumber(exec); }
void JSObject::defineSetter(ExecState* exec, const Identifier& propertyName, JSObject* setterFunction) { JSValuePtr object = getDirect(propertyName); if (object && object->isGetterSetter()) { ASSERT(m_structure->hasGetterSetterProperties()); asGetterSetter(object)->setSetter(setterFunction); return; } PutPropertySlot slot; GetterSetter* getterSetter = new (exec) GetterSetter; putDirect(propertyName, getterSetter, None, true, slot); // putDirect will change our Structure if we add a new property. For // getters and setters, though, we also need to change our Structure // if we override an existing non-getter or non-setter. if (slot.type() != PutPropertySlot::NewProperty) { if (!m_structure->isDictionary()) { RefPtr<Structure> structure = Structure::getterSetterTransition(m_structure); setStructure(structure.release()); } } m_structure->setHasGetterSetterProperties(true); getterSetter->setSetter(setterFunction); }
void JSObjectSetPrototype(JSContextRef, JSObjectRef object, JSValueRef value) { JSObject* jsObject = toJS(object); JSValuePtr jsValue = toJS(value); jsObject->setPrototype(jsValue->isObject() ? jsValue : jsNull()); }
JSValuePtr booleanProtoFuncValueOf(ExecState* exec, JSObject*, JSValuePtr thisValue, const ArgList&) { if (thisValue.isBoolean()) return thisValue; if (!thisValue.isObject(&BooleanObject::info)) return throwError(exec, TypeError); return asBooleanObject(thisValue)->internalValue(); }
void reportException(JSC::ExecState* exec, JSValuePtr exception) { UString errorMessage = exception.toString(exec); JSObject* exceptionObject = exception.toObject(exec); int lineNumber = exceptionObject->get(exec, Identifier(exec, "line")).toInt32(exec); UString exceptionSourceURL = exceptionObject->get(exec, Identifier(exec, "sourceURL")).toString(exec); exec->clearException(); ScriptExecutionContext* scriptExecutionContext = static_cast<JSDOMGlobalObject*>(exec->lexicalGlobalObject())->scriptExecutionContext(); scriptExecutionContext->reportException(errorMessage, lineNumber, exceptionSourceURL); }
bool JSValueIsObjectOfClass(JSContextRef, JSValueRef value, JSClassRef jsClass) { JSValuePtr jsValue = toJS(value); if (JSObject* o = jsValue.getObject()) { if (o->inherits(&JSCallbackObject<JSGlobalObject>::info)) return static_cast<JSCallbackObject<JSGlobalObject>*>(o)->inherits(jsClass); else if (o->inherits(&JSCallbackObject<JSObject>::info)) return static_cast<JSCallbackObject<JSObject>*>(o)->inherits(jsClass); } return false; }
void JSCustomSQLTransactionCallback::handleEvent(SQLTransaction* transaction, bool& raisedException) { ASSERT(m_data); ASSERT(m_data->callback()); ASSERT(m_data->frame()); if (!m_data->frame()->script()->isEnabled()) return; JSGlobalObject* globalObject = m_data->frame()->script()->globalObject(); ExecState* exec = globalObject->globalExec(); JSC::JSLock lock(false); JSValuePtr handleEventFunction = m_data->callback()->get(exec, Identifier(exec, "handleEvent")); CallData handleEventCallData; CallType handleEventCallType = handleEventFunction.getCallData(handleEventCallData); CallData callbackCallData; CallType callbackCallType = CallTypeNone; if (handleEventCallType == CallTypeNone) { callbackCallType = m_data->callback()->getCallData(callbackCallData); if (callbackCallType == CallTypeNone) { // FIXME: Should an exception be thrown here? return; } } RefPtr<JSCustomSQLTransactionCallback> protect(this); ArgList args; args.append(toJS(exec, transaction)); globalObject->startTimeoutCheck(); if (handleEventCallType != CallTypeNone) call(exec, handleEventFunction, handleEventCallType, handleEventCallData, m_data->callback(), args); else call(exec, m_data->callback(), callbackCallType, callbackCallData, m_data->callback(), args); globalObject->stopTimeoutCheck(); if (exec->hadException()) { reportCurrentException(exec); raisedException = true; } Document::updateDocumentsRendering(); }
void JSObject::mark() { JSOBJECT_MARK_BEGIN(); JSCell::mark(); m_structure->mark(); size_t storageSize = m_structure->propertyStorageSize(); for (size_t i = 0; i < storageSize; ++i) { JSValuePtr v = m_propertyStorage[i]; if (!v->marked()) v->mark(); } JSOBJECT_MARK_END(); }
bool JSStorage::deleteProperty(ExecState* exec, const Identifier& propertyName) { // Only perform the custom delete if the object doesn't have a native property by this name. // Since hasProperty() would end up calling canGetItemsForName() and be fooled, we need to check // the native property slots manually. PropertySlot slot; if (getStaticValueSlot<JSStorage, Base>(exec, s_info.propHashTable(exec), this, propertyName, slot)) return false; JSValuePtr prototype = this->prototype(); if (prototype->isObject() && asObject(prototype)->hasProperty(exec, propertyName)) return false; m_impl->removeItem(propertyName); return true; }
NPObject* ScriptController::createScriptObjectForPluginElement(HTMLPlugInElement* plugin) { // Can't create NPObjects when JavaScript is disabled if (!isEnabled()) return _NPN_CreateNoScriptObject(); // Create a JSObject bound to this element JSLock lock(false); ExecState* exec = globalObject()->globalExec(); JSValuePtr jsElementValue = toJS(exec, plugin); if (!jsElementValue || !jsElementValue.isObject()) return _NPN_CreateNoScriptObject(); // Wrap the JSObject in an NPObject return _NPN_CreateScriptObject(0, jsElementValue.getObject(), bindingRootObject()); }
PassRefPtr<NodeFilter> toNodeFilter(JSValuePtr value) { if (value.isObject(&JSNodeFilter::s_info)) return static_cast<JSNodeFilter*>(asObject(value))->impl(); return NodeFilter::create(JSNodeFilterCondition::create(value)); }
JSStringRef JSValueToStringCopy(JSContextRef ctx, JSValueRef value, JSValueRef* exception) { ExecState* exec = toJS(ctx); exec->globalData().heap.registerThread(); JSLock lock(exec); JSValuePtr jsValue = toJS(value); RefPtr<OpaqueJSString> stringRef(OpaqueJSString::create(jsValue.toString(exec))); if (exec->hadException()) { if (exception) *exception = toRef(exec->exception()); exec->clearException(); stringRef.clear(); } return stringRef.release().releaseRef(); }
bool JSObject::hasInstance(ExecState* exec, JSValuePtr value, JSValuePtr proto) { if (!proto->isObject()) { throwError(exec, TypeError, "instanceof called on an object with an invalid prototype property."); return false; } if (!value->isObject()) return false; JSObject* object = asObject(value); while ((object = object->prototype()->getObject())) { if (proto == object) return true; } return false; }
JSObjectRef JSValueToObject(JSContextRef ctx, JSValueRef value, JSValueRef* exception) { ExecState* exec = toJS(ctx); exec->globalData().heap.registerThread(); JSLock lock(exec); JSValuePtr jsValue = toJS(value); JSObjectRef objectRef = toRef(jsValue.toObject(exec)); if (exec->hadException()) { if (exception) *exception = toRef(exec->exception()); exec->clearException(); objectRef = 0; } return objectRef; }
double JSValueToNumber(JSContextRef ctx, JSValueRef value, JSValueRef* exception) { ExecState* exec = toJS(ctx); exec->globalData().heap.registerThread(); JSLock lock(exec); JSValuePtr jsValue = toJS(value); double number = jsValue.toNumber(exec); if (exec->hadException()) { if (exception) *exception = toRef(exec->exception()); exec->clearException(); number = NaN; } return number; }
bool JSCustomSQLTransactionErrorCallback::handleEvent(SQLError* error) { ASSERT(m_callback); ASSERT(m_frame); if (!m_frame->script()->isEnabled()) return true; JSGlobalObject* globalObject = m_frame->script()->globalObject(); ExecState* exec = globalObject->globalExec(); JSC::JSLock lock(false); JSValuePtr function = m_callback->get(exec, Identifier(exec, "handleEvent")); CallData callData; CallType callType = function.getCallData(callData); if (callType == CallTypeNone) { callType = m_callback->getCallData(callData); if (callType == CallTypeNone) { // FIXME: Should an exception be thrown here? return true; } function = m_callback; } RefPtr<JSCustomSQLTransactionErrorCallback> protect(this); ArgList args; args.append(toJS(exec, error)); JSValuePtr result; globalObject->startTimeoutCheck(); result = call(exec, function, callType, callData, m_callback, args); globalObject->stopTimeoutCheck(); if (exec->hadException()) reportCurrentException(exec); Document::updateDocumentsRendering(); return result.toBoolean(exec); }
PassRefPtr<JSEventListener> JSDOMGlobalObject::findOrCreateJSEventListener(ExecState*, JSValuePtr val, bool isInline) { if (JSEventListener* listener = findJSEventListener(val, isInline)) return listener; if (!val->isObject()) return 0; // The JSEventListener constructor adds it to our jsEventListeners map. return JSEventListener::create(asObject(val), this, isInline).get(); }
static ALWAYS_INLINE JSValuePtr callDefaultValueFunction(ExecState* exec, const JSObject* object, const Identifier& propertyName) { JSValuePtr function = object->get(exec, propertyName); CallData callData; CallType callType = function->getCallData(callData); if (callType == CallTypeNone) return exec->exception(); // Prevent "toString" and "valueOf" from observing execution if an exception // is pending. if (exec->hadException()) return exec->exception(); JSValuePtr result = call(exec, function, callType, callData, const_cast<JSObject*>(object), exec->emptyList()); ASSERT(!result->isGetterSetter()); if (exec->hadException()) return exec->exception(); if (result->isObject()) return noValue(); return result; }
bool UserObjectImp::getOwnPropertySlot(ExecState *exec, const Identifier& propertyName, PropertySlot& slot) { if (!fJSUserObject) return false; CFStringRef cfPropName = IdentifierToCFString(propertyName); JSUserObject *jsResult = fJSUserObject->CopyProperty(cfPropName); ReleaseCFType(cfPropName); if (jsResult) { slot.setCustom(this, userObjectGetter); jsResult->Release(); return true; } else { JSValuePtr kjsValue = toPrimitive(exec); if (!kjsValue.isUndefinedOrNull()) { JSObject* kjsObject = kjsValue.toObject(exec); if (kjsObject->getPropertySlot(exec, propertyName, slot)) return true; } } return JSObject::getOwnPropertySlot(exec, propertyName, slot); }
static PassRefPtr<PositionOptions> createPositionOptions(ExecState* exec, JSValuePtr value) { if (!value->isObject()) return 0; JSObject* object = asObject(value); JSValuePtr enableHighAccuracyValue = object->get(exec, Identifier(exec, "enableHighAccuracy")); if (exec->hadException()) return 0; bool enableHighAccuracy = enableHighAccuracyValue->toBoolean(exec); if (exec->hadException()) return 0; JSValuePtr timeoutValue = object->get(exec, Identifier(exec, "timeout")); if (exec->hadException()) return 0; unsigned timeout = timeoutValue->toUInt32(exec); if (exec->hadException()) return 0; return PositionOptions::create(enableHighAccuracy, timeout); }
bool JSStorage::customPut(ExecState* exec, const Identifier& propertyName, JSValuePtr value, PutPropertySlot&) { // Only perform the custom put if the object doesn't have a native property by this name. // Since hasProperty() would end up calling canGetItemsForName() and be fooled, we need to check // the native property slots manually. PropertySlot slot; if (getStaticValueSlot<JSStorage, Base>(exec, s_info.propHashTable(exec), this, propertyName, slot)) return false; JSValuePtr prototype = this->prototype(); if (prototype->isObject() && asObject(prototype)->hasProperty(exec, propertyName)) return false; String stringValue = valueToStringWithNullCheck(exec, value); if (exec->hadException()) return true; ExceptionCode ec = 0; impl()->setItem(propertyName, stringValue, ec); setDOMException(exec, ec); return true; }
void JSDocument::setLocation(ExecState* exec, JSValuePtr value) { Frame* frame = static_cast<Document*>(impl())->frame(); if (!frame) return; String str = value->toString(exec); // IE and Mozilla both resolve the URL relative to the source frame, // not the target frame. Frame* activeFrame = asJSDOMWindow(exec->dynamicGlobalObject())->impl()->frame(); if (activeFrame) str = activeFrame->document()->completeURL(str).string(); bool userGesture = activeFrame->script()->processingUserGesture(); frame->loader()->scheduleLocationChange(str, activeFrame->loader()->outgoingReferrer(), false, userGesture); }
JSValuePtr booleanProtoFuncToString(ExecState* exec, JSObject*, JSValuePtr thisValue, const ArgList&) { if (thisValue == jsBoolean(false)) return jsNontrivialString(exec, "false"); if (thisValue == jsBoolean(true)) return jsNontrivialString(exec, "true"); if (!thisValue.isObject(&BooleanObject::info)) return throwError(exec, TypeError); if (asBooleanObject(thisValue)->internalValue() == jsBoolean(false)) return jsNontrivialString(exec, "false"); ASSERT(asBooleanObject(thisValue)->internalValue() == jsBoolean(true)); return jsNontrivialString(exec, "true"); }
void JSHTMLOptionsCollection::setLength(ExecState* exec, JSValuePtr value) { HTMLOptionsCollection* imp = static_cast<HTMLOptionsCollection*>(impl()); ExceptionCode ec = 0; unsigned newLength = 0; double lengthValue = value->toNumber(exec); if (!isnan(lengthValue) && !isinf(lengthValue)) { if (lengthValue < 0.0) ec = INDEX_SIZE_ERR; else if (lengthValue > static_cast<double>(UINT_MAX)) newLength = UINT_MAX; else newLength = static_cast<unsigned>(lengthValue); } if (!ec) imp->setLength(newLength, ec); setDOMException(exec, ec); }
// Variant value must be released with NPReleaseVariantValue() void convertValueToNPVariant(ExecState* exec, JSValuePtr value, NPVariant* result) { JSLock lock(false); VOID_TO_NPVARIANT(*result); if (value.isString()) { UString ustring = value.toString(exec); CString cstring = ustring.UTF8String(); NPString string = { (const NPUTF8*)cstring.c_str(), static_cast<uint32_t>(cstring.size()) }; NPN_InitializeVariantWithStringCopy(result, &string); } else if (value.isNumber()) { DOUBLE_TO_NPVARIANT(value.toNumber(exec), *result); } else if (value.isBoolean()) { BOOLEAN_TO_NPVARIANT(value.toBoolean(exec), *result); } else if (value.isNull()) { NULL_TO_NPVARIANT(*result); } else if (value.isObject()) { JSObject* object = asObject(value); if (object->classInfo() == &RuntimeObjectImp::s_info) { RuntimeObjectImp* imp = static_cast<RuntimeObjectImp*>(object); CInstance* instance = static_cast<CInstance*>(imp->getInternalInstance()); if (instance) { NPObject* obj = instance->getObject(); _NPN_RetainObject(obj); OBJECT_TO_NPVARIANT(obj, *result); } } else { JSGlobalObject* globalObject = exec->dynamicGlobalObject(); RootObject* rootObject = findRootObject(globalObject); if (rootObject) { NPObject* npObject = _NPN_CreateScriptObject(0, object, rootObject); OBJECT_TO_NPVARIANT(npObject, *result); } } } }
JSValuePtr errorProtoFuncToString(ExecState* exec, JSObject*, JSValuePtr thisValue, const ArgList&) { JSObject* thisObj = thisValue->toThisObject(exec); UString s = "Error"; JSValuePtr v = thisObj->get(exec, exec->propertyNames().name); if (!v->isUndefined()) s = v->toString(exec); v = thisObj->get(exec, exec->propertyNames().message); if (!v->isUndefined()) { // Mozilla-compatible format. s += ": "; s += v->toString(exec); } return jsNontrivialString(exec, s); }
bool JSValueIsObject(JSContextRef, JSValueRef value) { JSValuePtr jsValue = toJS(value); return jsValue.isObject(); }