#ifndef Operations_h
#define Operations_h
#include "ExceptionHelpers.h"
#include "Interpreter.h"
#include "JSCJSValueInlines.h"
#include "JSProxy.h"
#include "JSString.h"
#include "StructureInlines.h"
namespace JSC {
NEVER_INLINE JSValue jsAddSlowCase(CallFrame*, JSValue, JSValue);
JSValue jsTypeStringForValue(CallFrame*, JSValue);
JSValue jsTypeStringForValue(VM&, JSGlobalObject*, JSValue);
bool jsIsObjectType(CallFrame*, JSValue);
bool jsIsFunctionType(JSValue);
ALWAYS_INLINE JSValue jsString(ExecState* exec, JSString* s1, JSString* s2)
{
VM& vm = exec->vm();
int32_t length1 = s1->length();
if (!length1)
return s2;
int32_t length2 = s2->length();
if (!length2)
return s1;
if (sumOverflows<int32_t>(length1, length2))
return throwOutOfMemoryError(exec);
return JSRopeString::create(vm, s1, s2);
}
ALWAYS_INLINE JSValue jsString(ExecState* exec, const String& u1, const String& u2, const String& u3)
{
VM* vm = &exec->vm();
int32_t length1 = u1.length();
int32_t length2 = u2.length();
int32_t length3 = u3.length();
if (length1 < 0 || length2 < 0 || length3 < 0)
return throwOutOfMemoryError(exec);
if (!length1)
return jsString(exec, jsString(vm, u2), jsString(vm, u3));
if (!length2)
return jsString(exec, jsString(vm, u1), jsString(vm, u3));
if (!length3)
return jsString(exec, jsString(vm, u1), jsString(vm, u2));
if (sumOverflows<int32_t>(length1, length2, length3))
return throwOutOfMemoryError(exec);
return JSRopeString::create(exec->vm(), jsString(vm, u1), jsString(vm, u2), jsString(vm, u3));
}
ALWAYS_INLINE JSValue jsString(ExecState* exec, Register* strings, unsigned count)
{
VM* vm = &exec->vm();
JSRopeString::RopeBuilder ropeBuilder(*vm);
for (unsigned i = 0; i < count; ++i) {
JSValue v = strings[i].jsValue();
if (!ropeBuilder.append(v.toString(exec)))
return throwOutOfMemoryError(exec);
}
return ropeBuilder.release();
}
ALWAYS_INLINE JSValue jsStringFromArguments(ExecState* exec, JSValue thisValue)
{
VM* vm = &exec->vm();
JSRopeString::RopeBuilder ropeBuilder(*vm);
ropeBuilder.append(thisValue.toString(exec));
for (unsigned i = 0; i < exec->argumentCount(); ++i) {
JSValue v = exec->argument(i);
if (!ropeBuilder.append(v.toString(exec)))
return throwOutOfMemoryError(exec);
}
return ropeBuilder.release();
}
template<bool leftFirst>
ALWAYS_INLINE bool jsLess(CallFrame* callFrame, JSValue v1, JSValue v2)
{
if (v1.isInt32() && v2.isInt32())
return v1.asInt32() < v2.asInt32();
if (v1.isNumber() && v2.isNumber())
return v1.asNumber() < v2.asNumber();
if (isJSString(v1) && isJSString(v2))
return codePointCompareLessThan(asString(v1)->value(callFrame), asString(v2)->value(callFrame));
double n1;
double n2;
JSValue p1;
JSValue p2;
bool wasNotString1;
bool wasNotString2;
if (leftFirst) {
wasNotString1 = v1.getPrimitiveNumber(callFrame, n1, p1);
wasNotString2 = v2.getPrimitiveNumber(callFrame, n2, p2);
} else {
wasNotString2 = v2.getPrimitiveNumber(callFrame, n2, p2);
wasNotString1 = v1.getPrimitiveNumber(callFrame, n1, p1);
}
if (wasNotString1 | wasNotString2)
return n1 < n2;
return codePointCompareLessThan(asString(p1)->value(callFrame), asString(p2)->value(callFrame));
}
template<bool leftFirst>
ALWAYS_INLINE bool jsLessEq(CallFrame* callFrame, JSValue v1, JSValue v2)
{
if (v1.isInt32() && v2.isInt32())
return v1.asInt32() <= v2.asInt32();
if (v1.isNumber() && v2.isNumber())
return v1.asNumber() <= v2.asNumber();
if (isJSString(v1) && isJSString(v2))
return !codePointCompareLessThan(asString(v2)->value(callFrame), asString(v1)->value(callFrame));
double n1;
double n2;
JSValue p1;
JSValue p2;
bool wasNotString1;
bool wasNotString2;
if (leftFirst) {
wasNotString1 = v1.getPrimitiveNumber(callFrame, n1, p1);
wasNotString2 = v2.getPrimitiveNumber(callFrame, n2, p2);
} else {
wasNotString2 = v2.getPrimitiveNumber(callFrame, n2, p2);
wasNotString1 = v1.getPrimitiveNumber(callFrame, n1, p1);
}
if (wasNotString1 | wasNotString2)
return n1 <= n2;
return !codePointCompareLessThan(asString(p2)->value(callFrame), asString(p1)->value(callFrame));
}
ALWAYS_INLINE JSValue jsAdd(CallFrame* callFrame, JSValue v1, JSValue v2)
{
if (v1.isNumber() && v2.isNumber())
return jsNumber(v1.asNumber() + v2.asNumber());
if (v1.isString() && !v2.isObject())
return jsString(callFrame, asString(v1), v2.toString(callFrame));
return jsAddSlowCase(callFrame, v1, v2);
}
#define InvalidPrototypeChain (std::numeric_limits<size_t>::max())
inline size_t normalizePrototypeChainForChainAccess(CallFrame* callFrame, JSValue base, JSValue slotBase, const Identifier& propertyName, PropertyOffset& slotOffset)
{
JSCell* cell = base.asCell();
size_t count = 0;
while (slotBase != cell) {
if (cell->isProxy())
return InvalidPrototypeChain;
if (cell->structure()->typeInfo().hasImpureGetOwnPropertySlot())
return InvalidPrototypeChain;
JSValue v = cell->structure()->prototypeForLookup(callFrame);
if (v.isNull())
return InvalidPrototypeChain;
cell = v.asCell();
if (cell->structure()->isDictionary()) {
asObject(cell)->flattenDictionaryObject(callFrame->vm());
if (slotBase == cell)
slotOffset = cell->structure()->get(callFrame->vm(), propertyName);
}
++count;
}
ASSERT(count);
return count;
}
inline size_t normalizePrototypeChain(CallFrame* callFrame, JSCell* base)
{
size_t count = 0;
while (1) {
if (base->isProxy())
return InvalidPrototypeChain;
JSValue v = base->structure()->prototypeForLookup(callFrame);
if (v.isNull())
return count;
base = v.asCell();
if (base->structure()->isDictionary())
asObject(base)->flattenDictionaryObject(callFrame->vm());
++count;
}
}
inline bool isPrototypeChainNormalized(JSGlobalObject* globalObject, Structure* structure)
{
for (;;) {
if (structure->typeInfo().type() == ProxyType)
return false;
JSValue v = structure->prototypeForLookup(globalObject);
if (v.isNull())
return true;
structure = v.asCell()->structure();
if (structure->isDictionary())
return false;
}
}
}
#endif // Operations_h