#include "CallFrame.h"
#include "GlobalEvalFunction.h"
+#include "Interpreter.h"
#include "JSGlobalObject.h"
#include "JSString.h"
-#include "Interpreter.h"
-#include "Parser.h"
-#include "dtoa.h"
+#include "JSStringBuilder.h"
#include "Lexer.h"
+#include "LiteralParser.h"
#include "Nodes.h"
+#include "Parser.h"
+#include "StringBuilder.h"
+#include "StringExtras.h"
+#include "dtoa.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
namespace JSC {
-static JSValuePtr encode(ExecState* exec, const ArgList& args, const char* doNotEscape)
+static JSValue encode(ExecState* exec, const ArgList& args, const char* doNotEscape)
{
- UString str = args.at(exec, 0).toString(exec);
+ UString str = args.at(0).toString(exec);
CString cstr = str.UTF8String(true);
- if (!cstr.c_str())
+ if (!cstr.data())
return throwError(exec, URIError, "String contained an illegal UTF-16 sequence.");
- UString result = "";
- const char* p = cstr.c_str();
- for (size_t k = 0; k < cstr.size(); k++, p++) {
+ JSStringBuilder builder;
+ const char* p = cstr.data();
+ for (size_t k = 0; k < cstr.length(); k++, p++) {
char c = *p;
if (c && strchr(doNotEscape, c))
- result.append(c);
+ builder.append(c);
else {
char tmp[4];
snprintf(tmp, sizeof(tmp), "%%%02X", static_cast<unsigned char>(c));
- result += tmp;
+ builder.append(tmp);
}
}
- return jsString(exec, result);
+ return builder.build(exec);
}
-static JSValuePtr decode(ExecState* exec, const ArgList& args, const char* doNotUnescape, bool strict)
+static JSValue decode(ExecState* exec, const ArgList& args, const char* doNotUnescape, bool strict)
{
- UString result = "";
- UString str = args.at(exec, 0).toString(exec);
+ JSStringBuilder builder;
+ UString str = args.at(0).toString(exec);
int k = 0;
int len = str.size();
const UChar* d = str.data();
charLen = 0;
else if (character >= 0x10000) {
// Convert to surrogate pair.
- result.append(static_cast<UChar>(0xD800 | ((character - 0x10000) >> 10)));
+ builder.append(static_cast<UChar>(0xD800 | ((character - 0x10000) >> 10)));
u = static_cast<UChar>(0xDC00 | ((character - 0x10000) & 0x3FF));
} else
u = static_cast<UChar>(character);
}
}
k++;
- result.append(c);
+ builder.append(c);
}
- return jsString(exec, result);
+ return builder.build(exec);
}
bool isStrWhiteSpace(UChar c)
}
if (number >= mantissaOverflowLowerBound) {
+ // FIXME: It is incorrect to use UString::ascii() here because it's not thread-safe.
if (radix == 10)
number = WTF::strtod(s.substr(firstDigitPosition, p - firstDigitPosition).ascii(), 0);
else if (radix == 2 || radix == 4 || radix == 8 || radix == 16 || radix == 32)
if (length - p >= 2 && data[p] == '0' && (data[p + 1] == 'x' || data[p + 1] == 'X'))
return 0;
+ // FIXME: UString::toDouble will ignore leading ASCII spaces, but we need to ignore
+ // other StrWhiteSpaceChar values as well.
return s.toDouble(true /*tolerant*/, false /* NaN for empty string */);
}
-JSValuePtr globalFuncEval(ExecState* exec, JSObject* function, JSValuePtr thisValue, const ArgList& args)
+JSValue JSC_HOST_CALL globalFuncEval(ExecState* exec, JSObject* function, JSValue thisValue, const ArgList& args)
{
JSObject* thisObject = thisValue.toThisObject(exec);
JSObject* unwrappedObject = thisObject->unwrappedObject();
if (!unwrappedObject->isGlobalObject() || static_cast<JSGlobalObject*>(unwrappedObject)->evalFunction() != function)
return throwError(exec, EvalError, "The \"this\" value passed to eval must be the global object from which eval originated");
- JSValuePtr x = args.at(exec, 0);
+ JSValue x = args.at(0);
if (!x.isString())
return x;
UString s = x.toString(exec);
- int errLine;
- UString errMsg;
-
- SourceCode source = makeSource(s);
- RefPtr<EvalNode> evalNode = exec->globalData().parser->parse<EvalNode>(exec, exec->dynamicGlobalObject()->debugger(), source, &errLine, &errMsg);
+ LiteralParser preparser(exec, s, LiteralParser::NonStrictJSON);
+ if (JSValue parsedObject = preparser.tryLiteralParse())
+ return parsedObject;
- if (!evalNode)
- return throwError(exec, SyntaxError, errMsg, errLine, source.provider()->asID(), NULL);
+ RefPtr<EvalExecutable> eval = EvalExecutable::create(exec, makeSource(s));
+ JSObject* error = eval->compile(exec, static_cast<JSGlobalObject*>(unwrappedObject)->globalScopeChain().node());
+ if (error)
+ return throwError(exec, error);
- return exec->interpreter()->execute(evalNode.get(), exec, thisObject, static_cast<JSGlobalObject*>(unwrappedObject)->globalScopeChain().node(), exec->exceptionSlot());
+ return exec->interpreter()->execute(eval.get(), exec, thisObject, static_cast<JSGlobalObject*>(unwrappedObject)->globalScopeChain().node(), exec->exceptionSlot());
}
-JSValuePtr globalFuncParseInt(ExecState* exec, JSObject*, JSValuePtr, const ArgList& args)
+JSValue JSC_HOST_CALL globalFuncParseInt(ExecState* exec, JSObject*, JSValue, const ArgList& args)
{
- JSValuePtr value = args.at(exec, 0);
- int32_t radix = args.at(exec, 1).toInt32(exec);
+ JSValue value = args.at(0);
+ int32_t radix = args.at(1).toInt32(exec);
+
+ if (radix != 0 && radix != 10)
+ return jsNumber(exec, parseInt(value.toString(exec), radix));
+
+ if (value.isInt32())
+ return value;
- if (value.isNumber() && (radix == 0 || radix == 10)) {
- if (value.isInt32Fast())
- return value;
- double d = value.uncheckedGetNumber();
+ if (value.isDouble()) {
+ double d = value.asDouble();
if (isfinite(d))
return jsNumber(exec, (d > 0) ? floor(d) : ceil(d));
if (isnan(d) || isinf(d))
- return jsNaN(&exec->globalData());
- return js0();
+ return jsNaN(exec);
+ return jsNumber(exec, 0);
}
return jsNumber(exec, parseInt(value.toString(exec), radix));
}
-JSValuePtr globalFuncParseFloat(ExecState* exec, JSObject*, JSValuePtr, const ArgList& args)
+JSValue JSC_HOST_CALL globalFuncParseFloat(ExecState* exec, JSObject*, JSValue, const ArgList& args)
{
- return jsNumber(exec, parseFloat(args.at(exec, 0).toString(exec)));
+ return jsNumber(exec, parseFloat(args.at(0).toString(exec)));
}
-JSValuePtr globalFuncIsNaN(ExecState* exec, JSObject*, JSValuePtr, const ArgList& args)
+JSValue JSC_HOST_CALL globalFuncIsNaN(ExecState* exec, JSObject*, JSValue, const ArgList& args)
{
- return jsBoolean(isnan(args.at(exec, 0).toNumber(exec)));
+ return jsBoolean(isnan(args.at(0).toNumber(exec)));
}
-JSValuePtr globalFuncIsFinite(ExecState* exec, JSObject*, JSValuePtr, const ArgList& args)
+JSValue JSC_HOST_CALL globalFuncIsFinite(ExecState* exec, JSObject*, JSValue, const ArgList& args)
{
- double n = args.at(exec, 0).toNumber(exec);
+ double n = args.at(0).toNumber(exec);
return jsBoolean(!isnan(n) && !isinf(n));
}
-JSValuePtr globalFuncDecodeURI(ExecState* exec, JSObject*, JSValuePtr, const ArgList& args)
+JSValue JSC_HOST_CALL globalFuncDecodeURI(ExecState* exec, JSObject*, JSValue, const ArgList& args)
{
static const char do_not_unescape_when_decoding_URI[] =
"#$&+,/:;=?@";
return decode(exec, args, do_not_unescape_when_decoding_URI, true);
}
-JSValuePtr globalFuncDecodeURIComponent(ExecState* exec, JSObject*, JSValuePtr, const ArgList& args)
+JSValue JSC_HOST_CALL globalFuncDecodeURIComponent(ExecState* exec, JSObject*, JSValue, const ArgList& args)
{
return decode(exec, args, "", true);
}
-JSValuePtr globalFuncEncodeURI(ExecState* exec, JSObject*, JSValuePtr, const ArgList& args)
+JSValue JSC_HOST_CALL globalFuncEncodeURI(ExecState* exec, JSObject*, JSValue, const ArgList& args)
{
static const char do_not_escape_when_encoding_URI[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
return encode(exec, args, do_not_escape_when_encoding_URI);
}
-JSValuePtr globalFuncEncodeURIComponent(ExecState* exec, JSObject*, JSValuePtr, const ArgList& args)
+JSValue JSC_HOST_CALL globalFuncEncodeURIComponent(ExecState* exec, JSObject*, JSValue, const ArgList& args)
{
static const char do_not_escape_when_encoding_URI_component[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
return encode(exec, args, do_not_escape_when_encoding_URI_component);
}
-JSValuePtr globalFuncEscape(ExecState* exec, JSObject*, JSValuePtr, const ArgList& args)
+JSValue JSC_HOST_CALL globalFuncEscape(ExecState* exec, JSObject*, JSValue, const ArgList& args)
{
static const char do_not_escape[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789"
"*+-./@_";
- UString result = "";
- UString s;
- UString str = args.at(exec, 0).toString(exec);
+ JSStringBuilder builder;
+ UString str = args.at(0).toString(exec);
const UChar* c = str.data();
- for (int k = 0; k < str.size(); k++, c++) {
+ for (unsigned k = 0; k < str.size(); k++, c++) {
int u = c[0];
if (u > 255) {
char tmp[7];
snprintf(tmp, sizeof(tmp), "%%u%04X", u);
- s = UString(tmp);
+ builder.append(tmp);
} else if (u != 0 && strchr(do_not_escape, static_cast<char>(u)))
- s = UString(c, 1);
+ builder.append(c, 1);
else {
char tmp[4];
snprintf(tmp, sizeof(tmp), "%%%02X", u);
- s = UString(tmp);
+ builder.append(tmp);
}
- result += s;
}
- return jsString(exec, result);
+ return builder.build(exec);
}
-JSValuePtr globalFuncUnescape(ExecState* exec, JSObject*, JSValuePtr, const ArgList& args)
+JSValue JSC_HOST_CALL globalFuncUnescape(ExecState* exec, JSObject*, JSValue, const ArgList& args)
{
- UString result = "";
- UString str = args.at(exec, 0).toString(exec);
+ StringBuilder builder;
+ UString str = args.at(0).toString(exec);
int k = 0;
int len = str.size();
while (k < len) {
const UChar* c = str.data() + k;
UChar u;
if (c[0] == '%' && k <= len - 6 && c[1] == 'u') {
- if (Lexer::isHexDigit(c[2]) && Lexer::isHexDigit(c[3]) && Lexer::isHexDigit(c[4]) && Lexer::isHexDigit(c[5])) {
+ if (isASCIIHexDigit(c[2]) && isASCIIHexDigit(c[3]) && isASCIIHexDigit(c[4]) && isASCIIHexDigit(c[5])) {
u = Lexer::convertUnicode(c[2], c[3], c[4], c[5]);
c = &u;
k += 5;
}
- } else if (c[0] == '%' && k <= len - 3 && Lexer::isHexDigit(c[1]) && Lexer::isHexDigit(c[2])) {
+ } else if (c[0] == '%' && k <= len - 3 && isASCIIHexDigit(c[1]) && isASCIIHexDigit(c[2])) {
u = UChar(Lexer::convertHex(c[1], c[2]));
c = &u;
k += 2;
}
k++;
- result.append(*c);
+ builder.append(*c);
}
- return jsString(exec, result);
+ return jsString(exec, builder.build());
}
#ifndef NDEBUG
-JSValuePtr globalFuncJSCPrint(ExecState* exec, JSObject*, JSValuePtr, const ArgList& args)
+JSValue JSC_HOST_CALL globalFuncJSCPrint(ExecState* exec, JSObject*, JSValue, const ArgList& args)
{
- CStringBuffer string;
- args.at(exec, 0).toString(exec).getCString(string);
+ CString string = args.at(0).toString(exec).UTF8String();
puts(string.data());
return jsUndefined();
}