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