1/*
2 * Copyright (C) 2012-2019 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 "UnlinkedGlobalCodeBlock.h"
29
30namespace JSC {
31
32class Decoder;
33class CachedModuleCodeBlock;
34
35class UnlinkedModuleProgramCodeBlock final : public UnlinkedGlobalCodeBlock {
36public:
37 typedef UnlinkedGlobalCodeBlock Base;
38 static constexpr unsigned StructureFlags = Base::StructureFlags | StructureIsImmortal;
39
40 template<typename CellType, SubspaceAccess mode>
41 static IsoSubspace* subspaceFor(VM& vm)
42 {
43 return vm.unlinkedModuleProgramCodeBlockSpace<mode>();
44 }
45
46 static UnlinkedModuleProgramCodeBlock* create(VM& vm, const ExecutableInfo& info, OptionSet<CodeGenerationMode> codeGenerationMode)
47 {
48 UnlinkedModuleProgramCodeBlock* instance = new (NotNull, allocateCell<UnlinkedModuleProgramCodeBlock>(vm.heap)) UnlinkedModuleProgramCodeBlock(vm, vm.unlinkedModuleProgramCodeBlockStructure.get(), info, codeGenerationMode);
49 instance->finishCreation(vm);
50 return instance;
51 }
52
53 static void destroy(JSCell*);
54
55 // This offset represents the constant register offset to the stored symbol table that represents the layout of the
56 // module environment. This symbol table is created by the byte code generator since the module environment includes
57 // the top-most lexical captured variables inside the module code. This means that, once the module environment is
58 // allocated and instantiated from this symbol table, it is titely coupled with the specific unlinked module program
59 // code block and the stored symbol table. So before executing the module code, we should not clear the unlinked module
60 // program code block in the module executable. This requirement is met because the garbage collector only clears
61 // unlinked code in (1) unmarked executables and (2) function executables.
62 //
63 // Since the function code may be executed repeatedly and the environment of each function execution is different,
64 // the function code need to allocate and instantiate the environment in the prologue of the function code. On the
65 // other hand, the module code is executed only once. So we can instantiate the module environment outside the module
66 // code. At that time, we construct the module environment by using the symbol table that is held by the module executable.
67 // The symbol table held by the executable is the cloned one from one in the unlinked code block. Instantiating the module
68 // environment before executing and linking the module code is required to link the imported bindings between the modules.
69 //
70 // The unlinked module program code block only holds the pre-cloned symbol table in its constant register pool. It does
71 // not hold the instantiated module environment. So while the module environment requires the specific unlinked module
72 // program code block, the unlinked module code block can be used for the module environment instantiated from this
73 // unlinked code block. There is 1:N relation between the unlinked module code block and the module environments. So the
74 // unlinked module program code block can be cached.
75 //
76 // On the other hand, the linked code block for the module environment includes the resolved references to the imported
77 // bindings. The imported binding references the other module environment, so the linked code block is titly coupled
78 // with the specific set of the module environments. Thus, the linked code block should not be cached.
79 int moduleEnvironmentSymbolTableConstantRegisterOffset() { return m_moduleEnvironmentSymbolTableConstantRegisterOffset; }
80 void setModuleEnvironmentSymbolTableConstantRegisterOffset(int offset)
81 {
82 m_moduleEnvironmentSymbolTableConstantRegisterOffset = offset;
83 }
84
85private:
86 friend CachedModuleCodeBlock;
87
88 UnlinkedModuleProgramCodeBlock(VM& vm, Structure* structure, const ExecutableInfo& info, OptionSet<CodeGenerationMode> codeGenerationMode)
89 : Base(vm, structure, ModuleCode, info, codeGenerationMode)
90 {
91 }
92
93 UnlinkedModuleProgramCodeBlock(Decoder&, const CachedModuleCodeBlock&);
94
95 int m_moduleEnvironmentSymbolTableConstantRegisterOffset { 0 };
96
97public:
98 static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue proto)
99 {
100 return Structure::create(vm, globalObject, proto, TypeInfo(UnlinkedModuleProgramCodeBlockType, StructureFlags), info());
101 }
102
103 DECLARE_INFO;
104};
105
106}
107