| 1 | /* |
| 2 | * Copyright (C) 2012-2018 Apple Inc. All Rights Reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #pragma once |
| 27 | |
| 28 | #include "CodeSpecializationKind.h" |
| 29 | #include "ConstructAbility.h" |
| 30 | #include "ExecutableInfo.h" |
| 31 | #include "ExpressionRangeInfo.h" |
| 32 | #include "Identifier.h" |
| 33 | #include "Intrinsic.h" |
| 34 | #include "JSCast.h" |
| 35 | #include "ParserModes.h" |
| 36 | #include "RegExp.h" |
| 37 | #include "SourceCode.h" |
| 38 | #include "VariableEnvironment.h" |
| 39 | #include <wtf/Optional.h> |
| 40 | |
| 41 | namespace JSC { |
| 42 | |
| 43 | class Decoder; |
| 44 | class FunctionMetadataNode; |
| 45 | class FunctionExecutable; |
| 46 | class ParserError; |
| 47 | class SourceProvider; |
| 48 | class UnlinkedFunctionCodeBlock; |
| 49 | class CachedFunctionExecutable; |
| 50 | |
| 51 | enum UnlinkedFunctionKind { |
| 52 | UnlinkedNormalFunction, |
| 53 | UnlinkedBuiltinFunction, |
| 54 | }; |
| 55 | |
| 56 | class UnlinkedFunctionExecutable final : public JSCell { |
| 57 | public: |
| 58 | friend class CodeCache; |
| 59 | friend class VM; |
| 60 | friend class CachedFunctionExecutable; |
| 61 | |
| 62 | typedef JSCell Base; |
| 63 | static const unsigned StructureFlags = Base::StructureFlags | StructureIsImmortal; |
| 64 | |
| 65 | template<typename CellType, SubspaceAccess> |
| 66 | static IsoSubspace* subspaceFor(VM& vm) |
| 67 | { |
| 68 | return &vm.unlinkedFunctionExecutableSpace.space; |
| 69 | } |
| 70 | |
| 71 | static UnlinkedFunctionExecutable* create(VM* vm, const SourceCode& source, FunctionMetadataNode* node, UnlinkedFunctionKind unlinkedFunctionKind, ConstructAbility constructAbility, JSParserScriptMode scriptMode, Optional<CompactVariableMap::Handle> parentScopeTDZVariables, DerivedContextType derivedContextType, bool isBuiltinDefaultClassConstructor = false) |
| 72 | { |
| 73 | UnlinkedFunctionExecutable* instance = new (NotNull, allocateCell<UnlinkedFunctionExecutable>(vm->heap)) |
| 74 | UnlinkedFunctionExecutable(vm, vm->unlinkedFunctionExecutableStructure.get(), source, node, unlinkedFunctionKind, constructAbility, scriptMode, WTFMove(parentScopeTDZVariables), derivedContextType, isBuiltinDefaultClassConstructor); |
| 75 | instance->finishCreation(*vm); |
| 76 | return instance; |
| 77 | } |
| 78 | |
| 79 | ~UnlinkedFunctionExecutable(); |
| 80 | |
| 81 | const Identifier& name() const { return m_name; } |
| 82 | const Identifier& ecmaName() const { return m_ecmaName; } |
| 83 | void setEcmaName(const Identifier& name) { m_ecmaName = name; } |
| 84 | unsigned parameterCount() const { return m_parameterCount; }; // Excluding 'this'! |
| 85 | SourceParseMode parseMode() const { return static_cast<SourceParseMode>(m_sourceParseMode); }; |
| 86 | |
| 87 | SourceCode classSource() const |
| 88 | { |
| 89 | if (m_rareData) |
| 90 | return m_rareData->m_classSource; |
| 91 | return SourceCode(); |
| 92 | } |
| 93 | void setClassSource(const SourceCode& source) |
| 94 | { |
| 95 | ensureRareData().m_classSource = source; |
| 96 | } |
| 97 | |
| 98 | bool isInStrictContext() const { return m_isInStrictContext; } |
| 99 | FunctionMode functionMode() const { return static_cast<FunctionMode>(m_functionMode); } |
| 100 | ConstructorKind constructorKind() const { return static_cast<ConstructorKind>(m_constructorKind); } |
| 101 | SuperBinding superBinding() const { return static_cast<SuperBinding>(m_superBinding); } |
| 102 | |
| 103 | unsigned lineCount() const { return m_lineCount; } |
| 104 | unsigned linkedStartColumn(unsigned parentStartColumn) const { return m_unlinkedBodyStartColumn + (!m_firstLineOffset ? parentStartColumn : 1); } |
| 105 | unsigned linkedEndColumn(unsigned startColumn) const { return m_unlinkedBodyEndColumn + (!m_lineCount ? startColumn : 1); } |
| 106 | |
| 107 | unsigned unlinkedFunctionNameStart() const { return m_unlinkedFunctionNameStart; } |
| 108 | unsigned unlinkedBodyStartColumn() const { return m_unlinkedBodyStartColumn; } |
| 109 | unsigned unlinkedBodyEndColumn() const { return m_unlinkedBodyEndColumn; } |
| 110 | unsigned startOffset() const { return m_startOffset; } |
| 111 | unsigned sourceLength() { return m_sourceLength; } |
| 112 | unsigned () const { return m_parametersStartOffset; } |
| 113 | unsigned typeProfilingStartOffset() const { return m_typeProfilingStartOffset; } |
| 114 | unsigned typeProfilingEndOffset() const { return m_typeProfilingEndOffset; } |
| 115 | void setInvalidTypeProfilingOffsets(); |
| 116 | |
| 117 | UnlinkedFunctionCodeBlock* unlinkedCodeBlockFor(CodeSpecializationKind); |
| 118 | |
| 119 | UnlinkedFunctionCodeBlock* unlinkedCodeBlockFor( |
| 120 | VM&, const SourceCode&, CodeSpecializationKind, OptionSet<CodeGenerationMode>, |
| 121 | ParserError&, SourceParseMode); |
| 122 | |
| 123 | static UnlinkedFunctionExecutable* fromGlobalCode( |
| 124 | const Identifier&, ExecState&, const SourceCode&, JSObject*& exception, |
| 125 | int overrideLineNumber, Optional<int> functionConstructorParametersEndPosition); |
| 126 | |
| 127 | SourceCode linkedSourceCode(const SourceCode&) const; |
| 128 | JS_EXPORT_PRIVATE FunctionExecutable* link(VM&, ScriptExecutable* topLevelExecutable, const SourceCode& parentSource, Optional<int> overrideLineNumber = WTF::nullopt, Intrinsic = NoIntrinsic); |
| 129 | |
| 130 | void clearCode(VM& vm) |
| 131 | { |
| 132 | m_unlinkedCodeBlockForCall.clear(); |
| 133 | m_unlinkedCodeBlockForConstruct.clear(); |
| 134 | vm.unlinkedFunctionExecutableSpace.set.remove(this); |
| 135 | } |
| 136 | |
| 137 | void recordParse(CodeFeatures features, bool hasCapturedVariables) |
| 138 | { |
| 139 | m_features = features; |
| 140 | m_hasCapturedVariables = hasCapturedVariables; |
| 141 | } |
| 142 | |
| 143 | CodeFeatures features() const { return m_features; } |
| 144 | bool hasCapturedVariables() const { return m_hasCapturedVariables; } |
| 145 | |
| 146 | static const bool needsDestruction = true; |
| 147 | static void destroy(JSCell*); |
| 148 | |
| 149 | bool isBuiltinFunction() const { return m_isBuiltinFunction; } |
| 150 | ConstructAbility constructAbility() const { return static_cast<ConstructAbility>(m_constructAbility); } |
| 151 | JSParserScriptMode scriptMode() const { return static_cast<JSParserScriptMode>(m_scriptMode); } |
| 152 | bool isClassConstructorFunction() const { return constructorKind() != ConstructorKind::None; } |
| 153 | bool isClass() const |
| 154 | { |
| 155 | if (!m_rareData) |
| 156 | return false; |
| 157 | return !m_rareData->m_classSource.isNull(); |
| 158 | } |
| 159 | |
| 160 | VariableEnvironment parentScopeTDZVariables() const |
| 161 | { |
| 162 | if (!m_rareData || !m_rareData->m_parentScopeTDZVariables) |
| 163 | return VariableEnvironment(); |
| 164 | return m_rareData->m_parentScopeTDZVariables.environment().toVariableEnvironment(); |
| 165 | } |
| 166 | |
| 167 | bool isArrowFunction() const { return isArrowFunctionParseMode(parseMode()); } |
| 168 | |
| 169 | JSC::DerivedContextType derivedContextType() const {return static_cast<JSC::DerivedContextType>(m_derivedContextType); } |
| 170 | |
| 171 | String sourceURLDirective() const |
| 172 | { |
| 173 | if (m_rareData) |
| 174 | return m_rareData->m_sourceURLDirective; |
| 175 | return String(); |
| 176 | } |
| 177 | String sourceMappingURLDirective() const |
| 178 | { |
| 179 | if (m_rareData) |
| 180 | return m_rareData->m_sourceMappingURLDirective; |
| 181 | return String(); |
| 182 | } |
| 183 | void setSourceURLDirective(const String& sourceURL) |
| 184 | { |
| 185 | ensureRareData().m_sourceURLDirective = sourceURL; |
| 186 | } |
| 187 | void setSourceMappingURLDirective(const String& sourceMappingURL) |
| 188 | { |
| 189 | ensureRareData().m_sourceMappingURLDirective = sourceMappingURL; |
| 190 | } |
| 191 | |
| 192 | struct RareData { |
| 193 | WTF_MAKE_STRUCT_FAST_ALLOCATED; |
| 194 | |
| 195 | SourceCode m_classSource; |
| 196 | String m_sourceURLDirective; |
| 197 | String m_sourceMappingURLDirective; |
| 198 | CompactVariableMap::Handle m_parentScopeTDZVariables; |
| 199 | }; |
| 200 | |
| 201 | private: |
| 202 | UnlinkedFunctionExecutable(VM*, Structure*, const SourceCode&, FunctionMetadataNode*, UnlinkedFunctionKind, ConstructAbility, JSParserScriptMode, Optional<CompactVariableMap::Handle>, JSC::DerivedContextType, bool isBuiltinDefaultClassConstructor); |
| 203 | UnlinkedFunctionExecutable(Decoder&, const CachedFunctionExecutable&); |
| 204 | |
| 205 | void decodeCachedCodeBlocks(); |
| 206 | |
| 207 | unsigned m_firstLineOffset : 31; |
| 208 | unsigned m_isInStrictContext : 1; |
| 209 | unsigned m_lineCount : 31; |
| 210 | unsigned m_hasCapturedVariables : 1; |
| 211 | unsigned m_unlinkedFunctionNameStart : 31; |
| 212 | unsigned m_isBuiltinFunction : 1; |
| 213 | unsigned m_unlinkedBodyStartColumn : 31; |
| 214 | unsigned m_isBuiltinDefaultClassConstructor : 1; |
| 215 | unsigned m_unlinkedBodyEndColumn : 31; |
| 216 | unsigned m_constructAbility: 1; |
| 217 | unsigned m_startOffset : 31; |
| 218 | unsigned m_scriptMode: 1; // JSParserScriptMode |
| 219 | unsigned m_sourceLength : 31; |
| 220 | unsigned m_superBinding : 1; |
| 221 | unsigned : 31; |
| 222 | unsigned m_isCached : 1; |
| 223 | unsigned m_typeProfilingStartOffset; |
| 224 | unsigned m_typeProfilingEndOffset; |
| 225 | unsigned m_parameterCount; |
| 226 | CodeFeatures m_features; |
| 227 | SourceParseMode m_sourceParseMode; |
| 228 | unsigned m_constructorKind : 2; |
| 229 | unsigned m_functionMode : 2; // FunctionMode |
| 230 | unsigned m_derivedContextType: 2; |
| 231 | |
| 232 | union { |
| 233 | WriteBarrier<UnlinkedFunctionCodeBlock> m_unlinkedCodeBlockForCall; |
| 234 | RefPtr<Decoder> m_decoder; |
| 235 | }; |
| 236 | |
| 237 | union { |
| 238 | WriteBarrier<UnlinkedFunctionCodeBlock> m_unlinkedCodeBlockForConstruct; |
| 239 | struct { |
| 240 | int32_t m_cachedCodeBlockForCallOffset; |
| 241 | int32_t m_cachedCodeBlockForConstructOffset; |
| 242 | }; |
| 243 | }; |
| 244 | |
| 245 | Identifier m_name; |
| 246 | Identifier m_ecmaName; |
| 247 | |
| 248 | RareData& ensureRareData() |
| 249 | { |
| 250 | if (LIKELY(m_rareData)) |
| 251 | return *m_rareData; |
| 252 | return ensureRareDataSlow(); |
| 253 | } |
| 254 | RareData& ensureRareDataSlow(); |
| 255 | |
| 256 | std::unique_ptr<RareData> m_rareData; |
| 257 | |
| 258 | protected: |
| 259 | static void visitChildren(JSCell*, SlotVisitor&); |
| 260 | |
| 261 | public: |
| 262 | static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue proto) |
| 263 | { |
| 264 | return Structure::create(vm, globalObject, proto, TypeInfo(UnlinkedFunctionExecutableType, StructureFlags), info()); |
| 265 | } |
| 266 | |
| 267 | DECLARE_EXPORT_INFO; |
| 268 | }; |
| 269 | |
| 270 | } // namespace JSC |
| 271 | |