diff options
author | Lorry Tar Creator <lorry-tar-importer@lorry> | 2017-06-27 06:07:23 +0000 |
---|---|---|
committer | Lorry Tar Creator <lorry-tar-importer@lorry> | 2017-06-27 06:07:23 +0000 |
commit | 1bf1084f2b10c3b47fd1a588d85d21ed0eb41d0c (patch) | |
tree | 46dcd36c86e7fbc6e5df36deb463b33e9967a6f7 /Source/JavaScriptCore/runtime/StringConstructor.cpp | |
parent | 32761a6cee1d0dee366b885b7b9c777e67885688 (diff) | |
download | WebKitGtk-tarball-master.tar.gz |
webkitgtk-2.16.5HEADwebkitgtk-2.16.5master
Diffstat (limited to 'Source/JavaScriptCore/runtime/StringConstructor.cpp')
-rw-r--r-- | Source/JavaScriptCore/runtime/StringConstructor.cpp | 81 |
1 files changed, 61 insertions, 20 deletions
diff --git a/Source/JavaScriptCore/runtime/StringConstructor.cpp b/Source/JavaScriptCore/runtime/StringConstructor.cpp index 207d8585a..9df4c9276 100644 --- a/Source/JavaScriptCore/runtime/StringConstructor.cpp +++ b/Source/JavaScriptCore/runtime/StringConstructor.cpp @@ -1,6 +1,6 @@ /* * Copyright (C) 1999-2001 Harri Porten (porten@kde.org) - * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. + * Copyright (C) 2004-2008, 2016 Apple Inc. All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -21,16 +21,18 @@ #include "config.h" #include "StringConstructor.h" -#include "Executable.h" +#include "Error.h" #include "JITCode.h" #include "JSFunction.h" #include "JSGlobalObject.h" -#include "Operations.h" +#include "JSCInlines.h" #include "StringPrototype.h" +#include <wtf/text/StringBuilder.h> namespace JSC { static EncodedJSValue JSC_HOST_CALL stringFromCharCode(ExecState*); +static EncodedJSValue JSC_HOST_CALL stringFromCodePoint(ExecState*); } @@ -38,11 +40,13 @@ static EncodedJSValue JSC_HOST_CALL stringFromCharCode(ExecState*); namespace JSC { -const ClassInfo StringConstructor::s_info = { "Function", &InternalFunction::s_info, 0, ExecState::stringConstructorTable, CREATE_METHOD_TABLE(StringConstructor) }; +const ClassInfo StringConstructor::s_info = { "Function", &InternalFunction::s_info, &stringConstructorTable, CREATE_METHOD_TABLE(StringConstructor) }; /* Source for StringConstructor.lut.h @begin stringConstructorTable - fromCharCode stringFromCharCode DontEnum|Function 1 + fromCharCode stringFromCharCode DontEnum|Function 1 FromCharCodeIntrinsic + fromCodePoint stringFromCodePoint DontEnum|Function 1 + raw JSBuiltin DontEnum|Function 1 @end */ @@ -60,21 +64,16 @@ void StringConstructor::finishCreation(VM& vm, StringPrototype* stringPrototype) putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(1), ReadOnly | DontEnum | DontDelete); } -bool StringConstructor::getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot &slot) -{ - return getStaticFunctionSlot<InternalFunction>(exec, ExecState::stringConstructorTable(exec->vm()), jsCast<StringConstructor*>(object), propertyName, slot); -} - // ------------------------------ Functions -------------------------------- static NEVER_INLINE JSValue stringFromCharCodeSlowCase(ExecState* exec) { unsigned length = exec->argumentCount(); UChar* buf; - PassRefPtr<StringImpl> impl = StringImpl::createUninitialized(length, buf); + auto impl = StringImpl::createUninitialized(length, buf); for (unsigned i = 0; i < length; ++i) buf[i] = static_cast<UChar>(exec->uncheckedArgument(i).toUInt32(exec)); - return jsString(exec, impl); + return jsString(exec, WTFMove(impl)); } static EncodedJSValue JSC_HOST_CALL stringFromCharCode(ExecState* exec) @@ -89,34 +88,76 @@ JSCell* JSC_HOST_CALL stringFromCharCode(ExecState* exec, int32_t arg) return jsSingleCharacterString(exec, arg); } -static EncodedJSValue JSC_HOST_CALL constructWithStringConstructor(ExecState* exec) +static EncodedJSValue JSC_HOST_CALL stringFromCodePoint(ExecState* exec) { - JSGlobalObject* globalObject = asInternalFunction(exec->callee())->globalObject(); VM& vm = exec->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + + unsigned length = exec->argumentCount(); + StringBuilder builder; + builder.reserveCapacity(length); + + for (unsigned i = 0; i < length; ++i) { + double codePointAsDouble = exec->uncheckedArgument(i).toNumber(exec); + RETURN_IF_EXCEPTION(scope, encodedJSValue()); + + uint32_t codePoint = static_cast<uint32_t>(codePointAsDouble); + + if (codePoint != codePointAsDouble || codePoint > UCHAR_MAX_VALUE) + return throwVMError(exec, scope, createRangeError(exec, ASCIILiteral("Arguments contain a value that is out of range of code points"))); + + if (U_IS_BMP(codePoint)) + builder.append(static_cast<UChar>(codePoint)); + else { + builder.append(U16_LEAD(codePoint)); + builder.append(U16_TRAIL(codePoint)); + } + } + + scope.release(); + return JSValue::encode(jsString(exec, builder.toString())); +} + +static EncodedJSValue JSC_HOST_CALL constructWithStringConstructor(ExecState* exec) +{ + JSGlobalObject* globalObject = asInternalFunction(exec->jsCallee())->globalObject(); + VM& vm = globalObject->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + + Structure* structure = InternalFunction::createSubclassStructure(exec, exec->newTarget(), globalObject->stringObjectStructure()); + RETURN_IF_EXCEPTION(scope, encodedJSValue()); if (!exec->argumentCount()) - return JSValue::encode(StringObject::create(vm, globalObject->stringObjectStructure())); - - return JSValue::encode(StringObject::create(vm, globalObject->stringObjectStructure(), exec->uncheckedArgument(0).toString(exec))); + return JSValue::encode(StringObject::create(vm, structure)); + JSString* str = exec->uncheckedArgument(0).toString(exec); + RETURN_IF_EXCEPTION(scope, encodedJSValue()); + return JSValue::encode(StringObject::create(vm, structure, str)); } ConstructType StringConstructor::getConstructData(JSCell*, ConstructData& constructData) { constructData.native.function = constructWithStringConstructor; - return ConstructTypeHost; + return ConstructType::Host; +} + +JSCell* stringConstructor(ExecState* exec, JSValue argument) +{ + if (argument.isSymbol()) + return jsNontrivialString(exec, asSymbol(argument)->descriptiveString()); + return argument.toString(exec); } static EncodedJSValue JSC_HOST_CALL callStringConstructor(ExecState* exec) { if (!exec->argumentCount()) return JSValue::encode(jsEmptyString(exec)); - return JSValue::encode(exec->uncheckedArgument(0).toString(exec)); + return JSValue::encode(stringConstructor(exec, exec->uncheckedArgument(0))); } CallType StringConstructor::getCallData(JSCell*, CallData& callData) { callData.native.function = callStringConstructor; - return CallTypeHost; + return CallType::Host; } } // namespace JSC |