#include "config.h"
#include "JSValueRef.h"
#include <wtf/Platform.h>
#include "APICast.h"
#include "JSCallbackObject.h"
#include <kjs/JSType.h>
#include <kjs/JSGlobalObject.h>
#include <kjs/internal.h>
#include <kjs/operations.h>
#include <kjs/protect.h>
#include <kjs/ustring.h>
#include <kjs/value.h>
#include <wtf/Assertions.h>
#include <algorithm> // for std::min
JSType JSValueGetType(JSContextRef, JSValueRef value)
{
KJS::JSValue* jsValue = toJS(value);
switch (jsValue->type()) {
case KJS::UndefinedType:
return kJSTypeUndefined;
case KJS::NullType:
return kJSTypeNull;
case KJS::BooleanType:
return kJSTypeBoolean;
case KJS::NumberType:
return kJSTypeNumber;
case KJS::StringType:
return kJSTypeString;
case KJS::ObjectType:
return kJSTypeObject;
default:
ASSERT(!"JSValueGetType: unknown type code.\n");
return kJSTypeUndefined;
}
}
using namespace KJS;
bool JSValueIsUndefined(JSContextRef, JSValueRef value)
{
JSValue* jsValue = toJS(value);
return jsValue->isUndefined();
}
bool JSValueIsNull(JSContextRef, JSValueRef value)
{
JSValue* jsValue = toJS(value);
return jsValue->isNull();
}
bool JSValueIsBoolean(JSContextRef, JSValueRef value)
{
JSValue* jsValue = toJS(value);
return jsValue->isBoolean();
}
bool JSValueIsNumber(JSContextRef, JSValueRef value)
{
JSValue* jsValue = toJS(value);
return jsValue->isNumber();
}
bool JSValueIsString(JSContextRef, JSValueRef value)
{
JSValue* jsValue = toJS(value);
return jsValue->isString();
}
bool JSValueIsObject(JSContextRef, JSValueRef value)
{
JSValue* jsValue = toJS(value);
return jsValue->isObject();
}
bool JSValueIsObjectOfClass(JSContextRef, JSValueRef value, JSClassRef jsClass)
{
JSValue* 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;
}
bool JSValueIsEqual(JSContextRef ctx, JSValueRef a, JSValueRef b, JSValueRef* exception)
{
JSLock lock;
ExecState* exec = toJS(ctx);
JSValue* jsA = toJS(a);
JSValue* jsB = toJS(b);
bool result = equal(exec, jsA, jsB); if (exec->hadException()) {
if (exception)
*exception = toRef(exec->exception());
exec->clearException();
}
return result;
}
bool JSValueIsStrictEqual(JSContextRef ctx, JSValueRef a, JSValueRef b)
{
JSLock lock;
ExecState* exec = toJS(ctx);
JSValue* jsA = toJS(a);
JSValue* jsB = toJS(b);
bool result = strictEqual(exec, jsA, jsB); ASSERT(!exec->hadException());
return result;
}
bool JSValueIsInstanceOfConstructor(JSContextRef ctx, JSValueRef value, JSObjectRef constructor, JSValueRef* exception)
{
JSLock lock;
ExecState* exec = toJS(ctx);
JSValue* jsValue = toJS(value);
JSObject* jsConstructor = toJS(constructor);
if (!jsConstructor->implementsHasInstance())
return false;
bool result = jsConstructor->hasInstance(exec, jsValue); if (exec->hadException()) {
if (exception)
*exception = toRef(exec->exception());
exec->clearException();
}
return result;
}
JSValueRef JSValueMakeUndefined(JSContextRef)
{
return toRef(jsUndefined());
}
JSValueRef JSValueMakeNull(JSContextRef)
{
return toRef(jsNull());
}
JSValueRef JSValueMakeBoolean(JSContextRef, bool value)
{
return toRef(jsBoolean(value));
}
JSValueRef JSValueMakeNumber(JSContextRef, double value)
{
JSLock lock;
return toRef(jsNumber(value));
}
JSValueRef JSValueMakeString(JSContextRef, JSStringRef string)
{
JSLock lock;
UString::Rep* rep = toJS(string);
return toRef(jsString(UString(rep)));
}
bool JSValueToBoolean(JSContextRef ctx, JSValueRef value)
{
ExecState* exec = toJS(ctx);
JSValue* jsValue = toJS(value);
return jsValue->toBoolean(exec);
}
double JSValueToNumber(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
{
JSLock lock;
JSValue* jsValue = toJS(value);
ExecState* exec = toJS(ctx);
double number = jsValue->toNumber(exec);
if (exec->hadException()) {
if (exception)
*exception = toRef(exec->exception());
exec->clearException();
number = NaN;
}
return number;
}
JSStringRef JSValueToStringCopy(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
{
JSLock lock;
JSValue* jsValue = toJS(value);
ExecState* exec = toJS(ctx);
JSStringRef stringRef = toRef(jsValue->toString(exec).rep()->ref());
if (exec->hadException()) {
if (exception)
*exception = toRef(exec->exception());
exec->clearException();
stringRef = 0;
}
return stringRef;
}
JSObjectRef JSValueToObject(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
{
JSLock lock;
ExecState* exec = toJS(ctx);
JSValue* jsValue = toJS(value);
JSObjectRef objectRef = toRef(jsValue->toObject(exec));
if (exec->hadException()) {
if (exception)
*exception = toRef(exec->exception());
exec->clearException();
objectRef = 0;
}
return objectRef;
}
void JSValueProtect(JSContextRef, JSValueRef value)
{
JSLock lock;
JSValue* jsValue = toJS(value);
gcProtect(jsValue);
}
void JSValueUnprotect(JSContextRef, JSValueRef value)
{
JSLock lock;
JSValue* jsValue = toJS(value);
gcUnprotect(jsValue);
}