blob: abf01ccb27b26ba7f63b4e3b98e269bb3696f820 [file] [log] [blame]
weinig@apple.comf0ea3552008-07-01 01:11:22 +00001/*
2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
mark.lam@apple.com7725e2a2019-02-27 05:43:34 +00003 * Copyright (C) 2003-2019 Apple Inc. All rights reserved.
weinig@apple.comf0ea3552008-07-01 01:11:22 +00004 * Copyright (C) 2007 Cameron Zwarich (cwzwarich@uwaterloo.ca)
5 * Copyright (C) 2007 Maks Orlovich
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 *
22 */
23
ryanhaddad@apple.com22104f52016-09-28 17:08:17 +000024#pragma once
weinig@apple.comf0ea3552008-07-01 01:11:22 +000025
utatane.tea@gmail.com7ab015d2017-11-06 14:40:08 +000026#include "CodeSpecializationKind.h"
mhahnenberg@apple.com30738a72012-10-03 17:51:28 +000027#include "JSDestructibleObject.h"
weinig@apple.comf0ea3552008-07-01 01:11:22 +000028
cwzwarich@webkit.org3f782f62008-09-08 01:28:33 +000029namespace JSC {
weinig@apple.comf0ea3552008-07-01 01:11:22 +000030
mark.lam@apple.com188640e2014-09-04 19:10:36 +000031class FunctionPrototype;
weinig@apple.comf0ea3552008-07-01 01:11:22 +000032
mark.lam@apple.com188640e2014-09-04 19:10:36 +000033class InternalFunction : public JSDestructibleObject {
utatane.tea@gmail.com7ab015d2017-11-06 14:40:08 +000034 friend class JIT;
35 friend class LLIntOffsetsExtractor;
mark.lam@apple.com188640e2014-09-04 19:10:36 +000036public:
37 typedef JSDestructibleObject Base;
mark.lam@apple.comd27553f2019-09-18 00:36:19 +000038 static constexpr unsigned StructureFlags = Base::StructureFlags | ImplementsHasInstance | ImplementsDefaultHasInstance | OverridesGetCallData;
commit-queue@webkit.org6c25c522011-08-09 20:46:17 +000039
ysuzuki@apple.com64b74db2019-02-05 06:32:08 +000040 template<typename CellType, SubspaceAccess>
fpizlo@apple.comdcb592d2018-04-19 19:33:03 +000041 static IsoSubspace* subspaceFor(VM& vm)
42 {
43 static_assert(sizeof(CellType) == sizeof(InternalFunction), "InternalFunction subclasses that add fields need to override subspaceFor<>()");
44 return &vm.internalFunctionSpace;
45 }
46
mark.lam@apple.com188640e2014-09-04 19:10:36 +000047 DECLARE_EXPORT_INFO;
mrowe@apple.comdb57abe2008-09-30 19:38:36 +000048
mark.lam@apple.comac502b32016-03-09 00:01:09 +000049 JS_EXPORT_PRIVATE static void visitChildren(JSCell*, SlotVisitor&);
50
fpizlo@apple.comcff2d0c2016-05-04 21:21:36 +000051 JS_EXPORT_PRIVATE const String& name();
mark.lam@apple.comd14283b2016-06-07 19:51:41 +000052 const String displayName(VM&);
53 const String calculatedDisplayName(VM&);
weinig@apple.comf0ea3552008-07-01 01:11:22 +000054
keith_miller@apple.com6bd24352016-01-11 22:40:00 +000055 static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue proto)
mark.lam@apple.com188640e2014-09-04 19:10:36 +000056 {
utatane.tea@gmail.com7ab015d2017-11-06 14:40:08 +000057 return Structure::create(vm, globalObject, proto, TypeInfo(InternalFunctionType, StructureFlags), info());
darin@apple.com5a494422008-10-18 23:08:12 +000058 }
59
utatane.tea@gmail.com62a46852017-05-27 23:21:53 +000060 static Structure* createSubclassStructure(ExecState*, JSValue newTarget, Structure*);
ysuzuki@apple.com48d4e532019-09-05 01:23:46 +000061 static Structure* createSubclassStructure(ExecState*, JSObject* baseCallee, JSValue newTarget, Structure*);
keith_miller@apple.com3793b132016-01-11 21:31:04 +000062
mark.lam@apple.coma925f8a2018-03-12 21:06:42 +000063 TaggedNativeFunction nativeFunctionFor(CodeSpecializationKind kind)
utatane.tea@gmail.com7ab015d2017-11-06 14:40:08 +000064 {
65 if (kind == CodeForCall)
mark.lam@apple.com7725e2a2019-02-27 05:43:34 +000066 return m_functionForCall;
utatane.tea@gmail.com7ab015d2017-11-06 14:40:08 +000067 ASSERT(kind == CodeForConstruct);
mark.lam@apple.com7725e2a2019-02-27 05:43:34 +000068 return m_functionForConstruct;
utatane.tea@gmail.com7ab015d2017-11-06 14:40:08 +000069 }
70
71 static ptrdiff_t offsetOfNativeFunctionFor(CodeSpecializationKind kind)
72 {
73 if (kind == CodeForCall)
74 return OBJECT_OFFSETOF(InternalFunction, m_functionForCall);
75 ASSERT(kind == CodeForConstruct);
76 return OBJECT_OFFSETOF(InternalFunction, m_functionForConstruct);
77 }
78
mark.lam@apple.com188640e2014-09-04 19:10:36 +000079protected:
utatane.tea@gmail.com7ab015d2017-11-06 14:40:08 +000080 JS_EXPORT_PRIVATE InternalFunction(VM&, Structure*, NativeFunction functionForCall, NativeFunction functionForConstruct);
mark.lam@apple.com188640e2014-09-04 19:10:36 +000081
utatane.tea@gmail.com1fd82922017-09-01 06:40:55 +000082 enum class NameVisibility { Visible, Anonymous };
ysuzuki@apple.com5f05b1e2019-03-08 19:33:51 +000083 enum class NameAdditionMode { WithStructureTransition, WithoutStructureTransition };
84 JS_EXPORT_PRIVATE void finishCreation(VM&, const String& name, NameVisibility = NameVisibility::Visible, NameAdditionMode = NameAdditionMode::WithStructureTransition);
mark.lam@apple.com188640e2014-09-04 19:10:36 +000085
utatane.tea@gmail.com62a46852017-05-27 23:21:53 +000086 JS_EXPORT_PRIVATE static Structure* createSubclassStructureSlow(ExecState*, JSValue newTarget, Structure*);
87
utatane.tea@gmail.com7ab015d2017-11-06 14:40:08 +000088 JS_EXPORT_PRIVATE static ConstructType getConstructData(JSCell*, ConstructData&);
89 JS_EXPORT_PRIVATE static CallType getCallData(JSCell*, CallData&);
90
mark.lam@apple.com7725e2a2019-02-27 05:43:34 +000091 TaggedNativeFunction m_functionForCall;
92 TaggedNativeFunction m_functionForConstruct;
mark.lam@apple.comac502b32016-03-09 00:01:09 +000093 WriteBarrier<JSString> m_originalName;
mark.lam@apple.com188640e2014-09-04 19:10:36 +000094};
95
utatane.tea@gmail.com62a46852017-05-27 23:21:53 +000096ALWAYS_INLINE Structure* InternalFunction::createSubclassStructure(ExecState* exec, JSValue newTarget, Structure* baseClass)
97{
ysuzuki@apple.com48d4e532019-09-05 01:23:46 +000098 return createSubclassStructure(exec, exec->jsCallee(), newTarget, baseClass);
99}
100
101ALWAYS_INLINE Structure* InternalFunction::createSubclassStructure(ExecState* exec, JSObject* baseCallee, JSValue newTarget, Structure* baseClass)
102{
utatane.tea@gmail.com62a46852017-05-27 23:21:53 +0000103 // We allow newTarget == JSValue() because the API needs to be able to create classes without having a real JS frame.
104 // Since we don't allow subclassing in the API we just treat newTarget == JSValue() as newTarget == exec->jsCallee()
ysuzuki@apple.com48d4e532019-09-05 01:23:46 +0000105 if (newTarget && newTarget != baseCallee)
utatane.tea@gmail.com62a46852017-05-27 23:21:53 +0000106 return createSubclassStructureSlow(exec, newTarget, baseClass);
107 return baseClass;
108}
109
cwzwarich@webkit.org3f782f62008-09-08 01:28:33 +0000110} // namespace JSC