本文整理汇总了C++中JSValue::asNumber方法的典型用法代码示例。如果您正苦于以下问题:C++ JSValue::asNumber方法的具体用法?C++ JSValue::asNumber怎么用?C++ JSValue::asNumber使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JSValue
的用法示例。
在下文中一共展示了JSValue::asNumber方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: newTypedArrayWithOneArgument
char* newTypedArrayWithOneArgument(
ExecState* exec, Structure* structure, EncodedJSValue encodedValue)
{
VM& vm = exec->vm();
NativeCallFrameTracer tracer(&vm, exec);
JSValue value = JSValue::decode(encodedValue);
if (JSArrayBuffer* jsBuffer = jsDynamicCast<JSArrayBuffer*>(value)) {
RefPtr<ArrayBuffer> buffer = jsBuffer->impl();
if (buffer->byteLength() % ViewClass::elementSize) {
vm.throwException(exec, createRangeError(exec, ASCIILiteral("ArrayBuffer length minus the byteOffset is not a multiple of the element size")));
return 0;
}
return bitwise_cast<char*>(
ViewClass::create(
exec, structure, buffer, 0, buffer->byteLength() / ViewClass::elementSize));
}
if (JSObject* object = jsDynamicCast<JSObject*>(value)) {
unsigned length = object->get(exec, vm.propertyNames->length).toUInt32(exec);
if (exec->hadException())
return 0;
ViewClass* result = ViewClass::createUninitialized(exec, structure, length);
if (!result)
return 0;
if (!result->set(exec, object, 0, length))
return 0;
return bitwise_cast<char*>(result);
}
int length;
if (value.isInt32())
length = value.asInt32();
else if (!value.isNumber()) {
vm.throwException(exec, createTypeError(exec, ASCIILiteral("Invalid array length argument")));
return 0;
} else {
length = static_cast<int>(value.asNumber());
if (length != value.asNumber()) {
vm.throwException(exec, createTypeError(exec, ASCIILiteral("Invalid array length argument (fractional lengths not allowed)")));
return 0;
}
}
if (length < 0) {
vm.throwException(exec, createRangeError(exec, ASCIILiteral("Requested length is negative")));
return 0;
}
return bitwise_cast<char*>(ViewClass::create(exec, structure, length));
}
示例2: sameValue
// See ES5.1 9.12
bool sameValue(ExecState* exec, JSValue a, JSValue b)
{
if (!a.isNumber())
return JSValue::strictEqual(exec, a, b);
if (!b.isNumber())
return false;
double x = a.asNumber();
double y = b.asNumber();
if (isnan(x))
return isnan(y);
return bitwise_cast<uint64_t>(x) == bitwise_cast<uint64_t>(y);
}
示例3: speculationFromValue
SpeculatedType speculationFromValue(JSValue value)
{
if (value.isEmpty())
return SpecEmpty;
if (value.isInt32())
return SpecInt32;
if (value.isDouble()) {
double number = value.asNumber();
if (number == number) {
int64_t asInt64 = static_cast<int64_t>(number);
if (asInt64 == number && (asInt64 || !std::signbit(number))
&& asInt64 < (static_cast<int64_t>(1) << 47)
&& asInt64 >= -(static_cast<int64_t>(1) << 47)) {
return SpecInt48AsDouble;
}
return SpecNonIntAsDouble;
}
return SpecDoubleNaN;
}
if (value.isCell())
return speculationFromCell(value.asCell());
if (value.isBoolean())
return SpecBoolean;
ASSERT(value.isUndefinedOrNull());
return SpecOther;
}
示例4: updateArithProfileForUnaryArithOp
static void updateArithProfileForUnaryArithOp(Instruction* pc, JSValue result, JSValue operand)
{
ArithProfile& profile = *bitwise_cast<ArithProfile*>(&pc[3].u.operand);
profile.observeLHS(operand);
ASSERT(result.isNumber());
if (!result.isInt32()) {
if (operand.isInt32())
profile.setObservedInt32Overflow();
double doubleVal = result.asNumber();
if (!doubleVal && std::signbit(doubleVal))
profile.setObservedNegZeroDouble();
else {
profile.setObservedNonNegZeroDouble();
// The Int52 overflow check here intentionally omits 1ll << 51 as a valid negative Int52 value.
// Therefore, we will get a false positive if the result is that value. This is intentionally
// done to simplify the checking algorithm.
static const int64_t int52OverflowPoint = (1ll << 51);
int64_t int64Val = static_cast<int64_t>(std::abs(doubleVal));
if (int64Val >= int52OverflowPoint)
profile.setObservedInt52Overflow();
}
}
}
示例5: if
static inline UString gap(ExecState* exec, JSValue space)
{
const unsigned maxGapLength = 10;
space = unwrapBoxedPrimitive(exec, space);
// If the space value is a number, create a gap string with that number of spaces.
if (space.isNumber()) {
double spaceCount = space.asNumber();
int count;
if (spaceCount > maxGapLength)
count = maxGapLength;
else if (!(spaceCount > 0))
count = 0;
else
count = static_cast<int>(spaceCount);
UChar spaces[maxGapLength];
for (int i = 0; i < count; ++i)
spaces[i] = ' ';
return UString(spaces, count);
}
// If the space value is a string, use it as the gap string, otherwise use no gap string.
UString spaces = space.getString(exec);
if (spaces.length() > maxGapLength) {
spaces = spaces.substringSharingImpl(0, maxGapLength);
}
return spaces;
}
示例6: putByVal
static inline void putByVal(ExecState* exec, JSValue baseValue, uint32_t index, JSValue value)
{
JSGlobalData* globalData = &exec->globalData();
if (isJSArray(baseValue)) {
JSArray* array = asArray(baseValue);
if (array->canSetIndex(index)) {
array->setIndex(*globalData, index, value);
return;
}
JSArray::putByIndex(array, exec, index, value, strict);
return;
}
if (isJSByteArray(baseValue) && asByteArray(baseValue)->canAccessIndex(index)) {
JSByteArray* byteArray = asByteArray(baseValue);
// FIXME: the JITstub used to relink this to an optimized form!
if (value.isInt32()) {
byteArray->setIndex(index, value.asInt32());
return;
}
if (value.isNumber()) {
byteArray->setIndex(index, value.asNumber());
return;
}
}
baseValue.putByIndex(exec, index, value, strict);
}
示例7: updateResultProfileForBinaryArithOp
static void updateResultProfileForBinaryArithOp(ExecState* exec, Instruction* pc, JSValue result, JSValue left, JSValue right)
{
CodeBlock* codeBlock = exec->codeBlock();
unsigned bytecodeOffset = codeBlock->bytecodeOffset(pc);
ResultProfile* profile = codeBlock->ensureResultProfile(bytecodeOffset);
if (result.isNumber()) {
if (!result.isInt32()) {
if (left.isInt32() && right.isInt32())
profile->setObservedInt32Overflow();
double doubleVal = result.asNumber();
if (!doubleVal && std::signbit(doubleVal))
profile->setObservedNegZeroDouble();
else {
profile->setObservedNonNegZeroDouble();
// The Int52 overflow check here intentionally omits 1ll << 51 as a valid negative Int52 value.
// Therefore, we will get a false positive if the result is that value. This is intentionally
// done to simplify the checking algorithm.
static const int64_t int52OverflowPoint = (1ll << 51);
int64_t int64Val = static_cast<int64_t>(std::abs(doubleVal));
if (int64Val >= int52OverflowPoint)
profile->setObservedInt52Overflow();
}
}
} else
profile->setObservedNonNumber();
}
示例8: isWithinPowerOfTwoForConstant
bool isWithinPowerOfTwoForConstant(Node* node)
{
JSValue immediateValue = node->asJSValue();
if (!immediateValue.isNumber())
return false;
double immediate = immediateValue.asNumber();
return immediate > -(static_cast<int64_t>(1) << power) && immediate < (static_cast<int64_t>(1) << power);
}
示例9: valueToDate
double valueToDate(ExecState* exec, JSValue value)
{
if (value.isNumber())
return value.asNumber();
if (!value.inherits(DateInstance::info()))
return std::numeric_limits<double>::quiet_NaN();
return static_cast<DateInstance*>(value.toObject(exec))->internalNumber();
}
示例10: null
static RefPtr<InspectorValue> jsToInspectorValue(ExecState* scriptState, JSValue value, int maxDepth)
{
if (!value) {
ASSERT_NOT_REACHED();
return nullptr;
}
if (!maxDepth)
return nullptr;
maxDepth--;
if (value.isNull() || value.isUndefined())
return InspectorValue::null();
if (value.isBoolean())
return InspectorValue::create(value.asBoolean());
if (value.isNumber() && value.isDouble())
return InspectorValue::create(value.asNumber());
if (value.isNumber() && value.isMachineInt())
return InspectorValue::create(static_cast<int>(value.asMachineInt()));
if (value.isString())
return InspectorValue::create(value.getString(scriptState));
if (value.isObject()) {
if (isJSArray(value)) {
Ref<InspectorArray> inspectorArray = InspectorArray::create();
JSArray* array = asArray(value);
unsigned length = array->length();
for (unsigned i = 0; i < length; i++) {
JSValue element = array->getIndex(scriptState, i);
RefPtr<InspectorValue> elementValue = jsToInspectorValue(scriptState, element, maxDepth);
if (!elementValue)
return nullptr;
inspectorArray->pushValue(WTFMove(elementValue));
}
return WTFMove(inspectorArray);
}
Ref<InspectorObject> inspectorObject = InspectorObject::create();
JSObject* object = value.getObject();
PropertyNameArray propertyNames(scriptState, PropertyNameMode::Strings);
object->methodTable()->getOwnPropertyNames(object, scriptState, propertyNames, EnumerationMode());
for (size_t i = 0; i < propertyNames.size(); i++) {
const Identifier& name = propertyNames[i];
JSValue propertyValue = object->get(scriptState, name);
RefPtr<InspectorValue> inspectorValue = jsToInspectorValue(scriptState, propertyValue, maxDepth);
if (!inspectorValue)
return nullptr;
inspectorObject->setValue(name.string(), WTFMove(inspectorValue));
}
return WTFMove(inspectorObject);
}
ASSERT_NOT_REACHED();
return nullptr;
}
示例11: null
static PassRefPtr<InspectorValue> jsToInspectorValue(ScriptState* scriptState, JSValue value, int maxDepth)
{
if (!value) {
ASSERT_NOT_REACHED();
return 0;
}
if (!maxDepth)
return 0;
maxDepth--;
if (value.isNull() || value.isUndefined())
return InspectorValue::null();
if (value.isBoolean())
return InspectorBasicValue::create(value.asBoolean());
if (value.isNumber())
return InspectorBasicValue::create(value.asNumber());
if (value.isString()) {
String s = value.getString(scriptState);
return InspectorString::create(String(s.characters(), s.length()));
}
if (value.isObject()) {
if (isJSArray(value)) {
RefPtr<InspectorArray> inspectorArray = InspectorArray::create();
JSArray* array = asArray(value);
unsigned length = array->length();
for (unsigned i = 0; i < length; i++) {
// FIXME: What if the array is in sparse mode? https://bugs.webkit.org/show_bug.cgi?id=95610
JSValue element = array->getIndexQuickly(i);
RefPtr<InspectorValue> elementValue = jsToInspectorValue(scriptState, element, maxDepth);
if (!elementValue)
return 0;
inspectorArray->pushValue(elementValue);
}
return inspectorArray;
}
RefPtr<InspectorObject> inspectorObject = InspectorObject::create();
JSObject* object = value.getObject();
PropertyNameArray propertyNames(scriptState);
object->methodTable()->getOwnPropertyNames(object, scriptState, propertyNames, ExcludeDontEnumProperties);
for (size_t i = 0; i < propertyNames.size(); i++) {
const Identifier& name = propertyNames[i];
JSValue propertyValue = object->get(scriptState, name);
RefPtr<InspectorValue> inspectorValue = jsToInspectorValue(scriptState, propertyValue, maxDepth);
if (!inspectorValue)
return 0;
inspectorObject->setValue(String(name.characters(), name.length()), inspectorValue);
}
return inspectorObject;
}
ASSERT_NOT_REACHED();
return 0;
}
示例12: functionProtoFuncBind
// 15.3.4.5 Function.prototype.bind (thisArg [, arg1 [, arg2, ...]])
EncodedJSValue JSC_HOST_CALL functionProtoFuncBind(ExecState* exec)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSGlobalObject* globalObject = exec->callee()->globalObject();
// Let Target be the this value.
JSValue target = exec->thisValue();
// If IsCallable(Target) is false, throw a TypeError exception.
CallData callData;
CallType callType = getCallData(target, callData);
if (callType == CallType::None)
return throwVMTypeError(exec, scope);
// Primitive values are not callable.
ASSERT(target.isObject());
JSObject* targetObject = asObject(target);
// Let A be a new (possibly empty) internal list of all of the argument values provided after thisArg (arg1, arg2 etc), in order.
size_t numBoundArgs = exec->argumentCount() > 1 ? exec->argumentCount() - 1 : 0;
JSArray* boundArgs;
if (numBoundArgs) {
boundArgs = JSArray::tryCreateUninitialized(vm, globalObject->arrayStructureForIndexingTypeDuringAllocation(ArrayWithContiguous), numBoundArgs);
if (!boundArgs)
return JSValue::encode(throwOutOfMemoryError(exec, scope));
for (size_t i = 0; i < numBoundArgs; ++i)
boundArgs->initializeIndex(vm, i, exec->argument(i + 1));
} else
boundArgs = nullptr;
// If the [[Class]] internal property of Target is "Function", then ...
// Else set the length own property of F to 0.
unsigned length = 0;
if (targetObject->hasOwnProperty(exec, exec->propertyNames().length)) {
if (exec->hadException())
return JSValue::encode(jsUndefined());
// a. Let L be the length property of Target minus the length of A.
// b. Set the length own property of F to either 0 or L, whichever is larger.
JSValue lengthValue = target.get(exec, exec->propertyNames().length);
if (lengthValue.isNumber()) {
unsigned targetLength = (unsigned)lengthValue.asNumber();
if (targetLength > numBoundArgs)
length = targetLength - numBoundArgs;
}
}
JSValue nameProp = target.get(exec, exec->propertyNames().name);
JSString* name = nameProp.isString() ? nameProp.toString(exec) : jsEmptyString(exec);
return JSValue::encode(JSBoundFunction::create(vm, exec, globalObject, targetObject, exec->argument(0), boundArgs, length, name->value(exec)));
}
示例13: predictionFromValue
PredictedType predictionFromValue(JSValue value)
{
if (value.isInt32())
return PredictInt32;
if (value.isDouble()) {
double number = value.asNumber();
if (number == number)
return PredictDoubleReal;
return PredictDoubleNaN;
}
if (value.isCell())
return predictionFromCell(value.asCell());
if (value.isBoolean())
return PredictBoolean;
ASSERT(value.isUndefinedOrNull());
return PredictOther;
}
示例14: speculationFromValue
SpeculatedType speculationFromValue(JSValue value)
{
if (value.isEmpty())
return SpecEmpty;
if (value.isInt32())
return SpecInt32;
if (value.isDouble()) {
double number = value.asNumber();
if (number != number)
return SpecDoubleNaN;
if (value.isMachineInt())
return SpecInt52AsDouble;
return SpecNonIntAsDouble;
}
if (value.isCell())
return speculationFromCell(value.asCell());
if (value.isBoolean())
return SpecBoolean;
ASSERT(value.isUndefinedOrNull());
return SpecOther;
}
示例15: convertValueToJValue
jvalue convertValueToJValue(ExecState* exec, RootObject* rootObject, JSValue value, JavaType javaType, const char* javaClassName)
{
JSLock lock(SilenceAssertionsOnly);
jvalue result;
memset(&result, 0, sizeof(jvalue));
switch (javaType) {
case JavaTypeArray:
case JavaTypeObject:
{
// FIXME: JavaJSObject::convertValueToJObject functionality is almost exactly the same,
// these functions should use common code.
if (value.isObject()) {
JSObject* object = asObject(value);
if (object->inherits(&JavaRuntimeObject::s_info)) {
// Unwrap a Java instance.
JavaRuntimeObject* runtimeObject = static_cast<JavaRuntimeObject*>(object);
JavaInstance* instance = runtimeObject->getInternalJavaInstance();
if (instance)
result.l = instance->javaInstance();
} else if (object->classInfo() == &RuntimeArray::s_info) {
// Input is a JavaScript Array that was originally created from a Java Array
RuntimeArray* imp = static_cast<RuntimeArray*>(object);
JavaArray* array = static_cast<JavaArray*>(imp->getConcreteArray());
result.l = array->javaArray();
} else if (object->classInfo() == &JSArray::s_info) {
// Input is a Javascript Array. We need to create it to a Java Array.
result.l = convertArrayInstanceToJavaArray(exec, asArray(value), javaClassName);
} else if ((!result.l && (!strcmp(javaClassName, "java.lang.Object")))
|| (!strcmp(javaClassName, "netscape.javascript.JSObject"))) {
// Wrap objects in JSObject instances.
JNIEnv* env = getJNIEnv();
jclass jsObjectClass = env->FindClass("sun/plugin/javascript/webkit/JSObject");
jmethodID constructorID = env->GetMethodID(jsObjectClass, "<init>", "(J)V");
if (constructorID) {
jlong nativeHandle = ptr_to_jlong(object);
rootObject->gcProtect(object);
result.l = env->NewObject(jsObjectClass, constructorID, nativeHandle);
}
}
}
// Create an appropriate Java object if target type is java.lang.Object.
if (!result.l && !strcmp(javaClassName, "java.lang.Object")) {
if (value.isString()) {
UString stringValue = asString(value)->value(exec);
JNIEnv* env = getJNIEnv();
jobject javaString = env->functions->NewString(env, (const jchar*)stringValue.characters(), stringValue.length());
result.l = javaString;
} else if (value.isNumber()) {
double doubleValue = value.asNumber();
JNIEnv* env = getJNIEnv();
jclass clazz = env->FindClass("java/lang/Double");
jmethodID constructor = env->GetMethodID(clazz, "<init>", "(D)V");
jobject javaDouble = env->functions->NewObject(env, clazz, constructor, doubleValue);
result.l = javaDouble;
} else if (value.isBoolean()) {
bool boolValue = value.asBoolean();
JNIEnv* env = getJNIEnv();
jclass clazz = env->FindClass("java/lang/Boolean");
jmethodID constructor = env->GetMethodID(clazz, "<init>", "(Z)V");
jobject javaBoolean = env->functions->NewObject(env, clazz, constructor, boolValue);
result.l = javaBoolean;
} else if (value.isUndefined()) {
UString stringValue = "undefined";
JNIEnv* env = getJNIEnv();
jobject javaString = env->functions->NewString(env, (const jchar*)stringValue.characters(), stringValue.length());
result.l = javaString;
}
}
// Convert value to a string if the target type is a java.lang.String, and we're not
// converting from a null.
if (!result.l && !strcmp(javaClassName, "java.lang.String")) {
if (!value.isNull()) {
UString stringValue = value.toString(exec);
JNIEnv* env = getJNIEnv();
jobject javaString = env->functions->NewString(env, (const jchar*)stringValue.characters(), stringValue.length());
result.l = javaString;
}
}
}
break;
case JavaTypeBoolean:
{
result.z = (jboolean)value.toNumber(exec);
}
break;
case JavaTypeByte:
{
result.b = (jbyte)value.toNumber(exec);
}
break;
case JavaTypeChar:
{
//.........这里部分代码省略.........