escargot/src/builtins/BuiltinFunction.cpp
Seonghyun Kim d92a795390 Update clang-format version to 20
Signed-off-by: Seonghyun Kim <sh8281.kim@samsung.com>
2025-07-01 18:39:28 +09:00

324 lines
17 KiB
C++

/*
* Copyright (c) 2016-present Samsung Electronics Co., Ltd
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/
#include "Escargot.h"
#include "runtime/GlobalObject.h"
#include "runtime/Context.h"
#include "runtime/VMInstance.h"
#include "runtime/NativeFunctionObject.h"
#include "runtime/BoundFunctionObject.h"
#include "runtime/ScriptFunctionObject.h"
#include "runtime/ScriptClassConstructorFunctionObject.h"
#include "parser/Lexer.h"
namespace Escargot {
static Value builtinFunctionEmptyFunction(ExecutionState& state, Value thisValue, size_t argc, Value* argv, Optional<Object*> newTarget)
{
return Value();
}
static Value builtinFunctionConstructor(ExecutionState& state, Value thisValue, size_t argc, Value* argv, Optional<Object*> newTarget)
{
if (newTarget.hasValue() && UNLIKELY((bool)state.context()->securityPolicyCheckCallback())) {
Value checkMSG = state.context()->securityPolicyCheckCallback()(state, false);
if (!checkMSG.isEmpty()) {
ASSERT(checkMSG.isString());
ErrorObject::throwBuiltinError(state, ErrorCode::EvalError, checkMSG.asString());
return Value();
}
}
size_t argumentVectorCount = argc > 1 ? argc - 1 : 0;
Value sourceValue = argc >= 1 ? argv[argc - 1] : Value(String::emptyString());
auto functionSource = FunctionObject::createDynamicFunctionScript(state, state.context()->staticStrings().anonymous, argumentVectorCount, argv, sourceValue, false, false, false, false);
// Let proto be ? GetPrototypeFromConstructor(newTarget, fallbackProto).
if (!newTarget.hasValue()) {
newTarget = state.resolveCallee();
}
Object* proto = Object::getPrototypeFromConstructor(state, newTarget.value(), [](ExecutionState& state, Context* constructorRealm) -> Object* {
return constructorRealm->globalObject()->functionPrototype();
});
ScriptFunctionObject* result = new ScriptFunctionObject(state, proto, functionSource.codeBlock, functionSource.outerEnvironment, true, false);
return result;
}
// https://www.ecma-international.org/ecma-262/10.0/index.html#sec-function.prototype.tostring
static Value builtinFunctionToString(ExecutionState& state, Value thisValue, size_t argc, Value* argv, Optional<Object*> newTarget)
{
if (LIKELY(thisValue.isObject())) {
Object* func = thisValue.asObject();
if (LIKELY(func->isFunctionObject())) {
FunctionObject* fn = func->asFunctionObject();
if (fn->isScriptClassConstructorFunctionObject()) {
return fn->asScriptFunctionObject()->asScriptClassConstructorFunctionObject()->classSourceCode();
} else {
StringBuilder builder;
if (fn->isScriptFunctionObject()) {
StringView src = fn->asScriptFunctionObject()->interpretedCodeBlock()->src();
size_t length = src.length();
while (length > 0 && EscargotLexer::isWhiteSpaceOrLineTerminator(src[length - 1])) {
length--;
}
builder.appendString(new StringView(src, 0, length), &state);
} else {
ASSERT(fn->isNativeFunctionObject());
builder.appendString("function ");
builder.appendString(fn->codeBlock()->functionName().string());
builder.appendString("() { [native code] }");
}
return builder.finalize(&state);
}
}
if (func->isBoundFunctionObject() || func->isCallable()) {
StringBuilder builder;
builder.appendString("function () { [native code] }");
return builder.finalize(&state);
}
}
ErrorObject::throwBuiltinError(state, ErrorCode::TypeError, state.context()->staticStrings().Function.string(), true, state.context()->staticStrings().toString.string(), ErrorObject::Messages::GlobalObject_ThisNotFunctionObject);
return Value();
}
static Value builtinFunctionApply(ExecutionState& state, Value thisValue, size_t argc, Value* argv, Optional<Object*> newTarget)
{
if (!thisValue.isCallable()) {
ErrorObject::throwBuiltinError(state, ErrorCode::TypeError, state.context()->staticStrings().Function.string(), true, state.context()->staticStrings().apply.string(), ErrorObject::Messages::GlobalObject_ThisNotFunctionObject);
}
Value thisArg = argv[0];
Value argArray = argv[1];
size_t arrlen = 0;
Value* arguments = nullptr;
ValueVector argList;
if (argArray.isUndefinedOrNull()) {
// TODO
} else {
argList = Object::createListFromArrayLike(state, argArray);
arrlen = argList.size();
arguments = argList.data();
}
return Object::call(state, thisValue, thisArg, arrlen, arguments);
}
static Value builtinFunctionCall(ExecutionState& state, Value thisValue, size_t argc, Value* argv, Optional<Object*> newTarget)
{
if (!thisValue.isCallable()) {
ErrorObject::throwBuiltinError(state, ErrorCode::TypeError, state.context()->staticStrings().Function.string(), true, state.context()->staticStrings().apply.string(), ErrorObject::Messages::GlobalObject_ThisNotFunctionObject);
}
Value thisArg = argv[0];
size_t arrlen = argc > 0 ? argc - 1 : 0;
Value* arguments = ALLOCA(sizeof(Value) * arrlen, Value);
for (size_t i = 0; i < arrlen; i++) {
arguments[i] = argv[i + 1];
}
return Object::call(state, thisValue, thisArg, arrlen, arguments);
}
// https://www.ecma-international.org/ecma-262/6.0/#sec-function.prototype.bind
static Value builtinFunctionBind(ExecutionState& state, Value thisValue, size_t argc, Value* argv, Optional<Object*> newTarget)
{
// If IsCallable(Target) is false, throw a TypeError exception.
if (!thisValue.isCallable()) {
ErrorObject::throwBuiltinError(state, ErrorCode::TypeError, state.context()->staticStrings().Function.string(), true, state.context()->staticStrings().bind.string(), ErrorObject::Messages::GlobalObject_ThisNotFunctionObject);
}
// Let Target be the this value.
Object* target = thisValue.asObject();
// Let args be a new (possibly empty) List consisting of all of the argument values provided after thisArg in order.
Value boundThis = argv[0];
size_t boundArgc = (argc > 0) ? argc - 1 : 0;
Value* boundArgv = (boundArgc > 0) ? argv + 1 : nullptr;
// BoundFunctionObject* F = new BoundFunctionObject(state, thisValue, boundThis, boundArgc, boundArgv);
// Let targetHasLength be HasOwnProperty(Target, "length").
bool targetHasLength = target->hasOwnProperty(state, ObjectPropertyName(state.context()->staticStrings().length));
double length = 0;
// If targetHasLength is true, then
if (targetHasLength) {
// Let targetLen be Get(Target, "length").
Value targetLen = target->get(state, ObjectPropertyName(state.context()->staticStrings().length)).value(state, target);
// If Type(targetLen) is not Number, let L be 0.
// Else Let targetLen be ToInteger(targetLen).
// Let L be the larger of 0 and the result of targetLen minus the number of elements of args.
if (targetLen.isNumber()) {
length = std::max(0.0, targetLen.toInteger(state) - boundArgc);
}
}
// F->defineOwnPropertyThrowsException(state, ObjectPropertyName(state.context()->staticStrings().length),
// ObjectPropertyDescriptor(Value(length), (ObjectPropertyDescriptor::PresentAttribute)(ObjectPropertyDescriptor::ConfigurablePresent)));
// Let targetName be Get(Target, "name").
Value targetName = target->get(state, ObjectPropertyName(state.context()->staticStrings().name)).value(state, target);
// If Type(targetName) is not String, let targetName be the empty string.
if (!targetName.isString()) {
targetName = String::emptyString();
}
StringBuilder builder;
builder.appendString("bound ");
builder.appendString(targetName.asString());
// F->defineOwnPropertyThrowsException(state, ObjectPropertyName(state.context()->staticStrings().name),
// ObjectPropertyDescriptor(Value(builder.finalize(&state)), (ObjectPropertyDescriptor::PresentAttribute)(ObjectPropertyDescriptor::ConfigurablePresent)));
// Let F be BoundFunctionCreate(Target, thisArg, args).
// Let status be DefinePropertyOrThrow(F, "length", PropertyDescriptor {[[Value]]: L, [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: true}).
// Perform SetFunctionName(F, targetName, "bound").
// Return F.
return new BoundFunctionObject(state, target, boundThis, boundArgc, boundArgv, Value(Value::DoubleToIntConvertibleTestNeeds, length), Value(builder.finalize(&state)));
}
static Value builtinFunctionHasInstanceOf(ExecutionState& state, Value thisValue, size_t argc, Value* argv, Optional<Object*> newTarget)
{
if (!thisValue.isObject()) {
return Value(false);
}
return Value(thisValue.asObject()->hasInstance(state, argv[0]));
}
static Value builtinCallerAndArgumentsGetterSetter(ExecutionState& state, Value thisValue, size_t argc, Value* argv, Optional<Object*> newTarget)
{
FunctionObject* targetFunction = nullptr;
bool needThrow = false;
if (thisValue.isCallable()) {
if (thisValue.isFunction()) {
targetFunction = thisValue.asFunction();
if (targetFunction->isScriptFunctionObject()) {
InterpretedCodeBlock* codeBlock = targetFunction->asScriptFunctionObject()->interpretedCodeBlock();
if (codeBlock->isStrict() || codeBlock->isArrowFunctionExpression() || codeBlock->isGenerator()) {
needThrow = true;
}
} else {
ASSERT(targetFunction->isNativeFunctionObject());
if (targetFunction->asNativeFunctionObject()->nativeCodeBlock()->isStrict()) {
needThrow = true;
}
}
} else if (thisValue.isObject()) {
auto object = thisValue.asObject();
if (object->isBoundFunctionObject()) {
needThrow = true;
}
}
} else if (!thisValue.isPrimitive() && !thisValue.isObject()) {
needThrow = true;
}
if (needThrow) {
ErrorObject::throwBuiltinError(state, ErrorCode::TypeError, "'caller' and 'arguments' restrict properties may not be accessed on strict mode functions or the arguments objects for calls to them");
}
bool inStrict = false;
ExecutionState* p = state.parent();
while (p) {
if (p->inStrictMode()) {
inStrict = true;
break;
}
p = p->parent();
}
if (targetFunction && !inStrict) {
bool foundTargetFunction = false;
ExecutionState* p = &state;
while (p) {
if (foundTargetFunction) {
auto callee = p->resolveCallee();
if (callee != targetFunction) {
if (callee) {
return Value(callee);
} else {
return Value(Value::Null);
}
}
} else {
if (p->resolveCallee() == targetFunction) {
foundTargetFunction = true;
}
}
p = p->parent();
}
}
return Value(Value::Null);
}
void GlobalObject::initializeFunction(ExecutionState& state)
{
// Function should be installed at the start time
installFunction(state);
}
void GlobalObject::installFunction(ExecutionState& state)
{
m_functionPrototype = new NativeFunctionObject(state, NativeFunctionInfo(AtomicString(), builtinFunctionEmptyFunction, 0, NativeFunctionInfo::Strict),
NativeFunctionObject::__ForGlobalBuiltin__);
m_functionPrototype->setGlobalIntrinsicObject(state, true);
m_function = new NativeFunctionObject(state, NativeFunctionInfo(state.context()->staticStrings().Function, builtinFunctionConstructor, 1), NativeFunctionObject::__ForBuiltinConstructor__);
m_function->setGlobalIntrinsicObject(state);
m_function->setFunctionPrototype(state, m_functionPrototype);
m_functionPrototype->directDefineOwnProperty(state, ObjectPropertyName(state.context()->staticStrings().constructor),
ObjectPropertyDescriptor(m_function, (ObjectPropertyDescriptor::PresentAttribute)(ObjectPropertyDescriptor::WritablePresent | ObjectPropertyDescriptor::ConfigurablePresent)));
m_functionPrototype->directDefineOwnProperty(state, ObjectPropertyName(state.context()->staticStrings().toString),
ObjectPropertyDescriptor(new NativeFunctionObject(state, NativeFunctionInfo(state.context()->staticStrings().toString, builtinFunctionToString, 0, NativeFunctionInfo::Strict)), (ObjectPropertyDescriptor::PresentAttribute)(ObjectPropertyDescriptor::WritablePresent | ObjectPropertyDescriptor::ConfigurablePresent)));
m_functionApply = new NativeFunctionObject(state, NativeFunctionInfo(state.context()->staticStrings().apply, builtinFunctionApply, 2, NativeFunctionInfo::Strict));
m_functionPrototype->directDefineOwnProperty(state, ObjectPropertyName(state.context()->staticStrings().apply),
ObjectPropertyDescriptor(m_functionApply, (ObjectPropertyDescriptor::PresentAttribute)(ObjectPropertyDescriptor::WritablePresent | ObjectPropertyDescriptor::ConfigurablePresent)));
m_functionPrototype->directDefineOwnProperty(state, ObjectPropertyName(state.context()->staticStrings().call),
ObjectPropertyDescriptor(new NativeFunctionObject(state, NativeFunctionInfo(state.context()->staticStrings().call, builtinFunctionCall, 1, NativeFunctionInfo::Strict)), (ObjectPropertyDescriptor::PresentAttribute)(ObjectPropertyDescriptor::WritablePresent | ObjectPropertyDescriptor::ConfigurablePresent)));
m_functionPrototype->directDefineOwnProperty(state, ObjectPropertyName(state.context()->staticStrings().bind),
ObjectPropertyDescriptor(new NativeFunctionObject(state, NativeFunctionInfo(state.context()->staticStrings().bind, builtinFunctionBind, 1, NativeFunctionInfo::Strict)), (ObjectPropertyDescriptor::PresentAttribute)(ObjectPropertyDescriptor::WritablePresent | ObjectPropertyDescriptor::ConfigurablePresent)));
m_functionPrototype->directDefineOwnProperty(state, ObjectPropertyName(state, Value(state.context()->vmInstance()->globalSymbols().hasInstance)),
ObjectPropertyDescriptor(new NativeFunctionObject(state, NativeFunctionInfo(AtomicString(state, String::fromASCII("[Symbol.hasInstance]")), builtinFunctionHasInstanceOf, 1, NativeFunctionInfo::Strict)), (ObjectPropertyDescriptor::PresentAttribute)(ObjectPropertyDescriptor::NonWritablePresent | ObjectPropertyDescriptor::NonConfigurablePresent)));
// 8.2.2 - 12
// AddRestrictedFunctionProperties(funcProto, realmRec).
m_callerAndArgumentsGetterSetter = new NativeFunctionObject(state, NativeFunctionInfo(state.context()->staticStrings().caller, builtinCallerAndArgumentsGetterSetter, 0, NativeFunctionInfo::Strict));
{
JSGetterSetter gs(m_callerAndArgumentsGetterSetter, m_callerAndArgumentsGetterSetter);
ObjectPropertyDescriptor desc(gs, ObjectPropertyDescriptor::ConfigurablePresent);
m_functionPrototype->directDefineOwnProperty(state, ObjectPropertyName(state.context()->staticStrings().caller), desc);
m_functionPrototype->directDefineOwnProperty(state, ObjectPropertyName(state.context()->staticStrings().arguments), desc);
}
directDefineOwnProperty(state, ObjectPropertyName(state.context()->staticStrings().Function),
ObjectPropertyDescriptor(m_function, (ObjectPropertyDescriptor::PresentAttribute)(ObjectPropertyDescriptor::WritablePresent | ObjectPropertyDescriptor::ConfigurablePresent)));
}
} // namespace Escargot