OLD | NEW |
1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE.md file. | 3 // BSD-style license that can be found in the LICENSE.md file. |
4 | 4 |
5 #include <stdlib.h> | 5 #include <stdlib.h> |
6 | 6 |
7 #include "src/vm/fletch_api_impl.h" | 7 #include "src/vm/dartino_api_impl.h" |
8 | 8 |
9 #include "src/shared/assert.h" | 9 #include "src/shared/assert.h" |
10 #ifdef FLETCH_ENABLE_LIVE_CODING | 10 #ifdef DARTINO_ENABLE_LIVE_CODING |
11 #include "src/shared/connection.h" | 11 #include "src/shared/connection.h" |
12 #endif | 12 #endif |
13 #include "src/shared/fletch.h" | 13 #include "src/shared/dartino.h" |
14 #include "src/shared/list.h" | 14 #include "src/shared/list.h" |
15 | 15 |
16 #include "src/vm/ffi.h" | 16 #include "src/vm/ffi.h" |
17 #include "src/vm/program.h" | 17 #include "src/vm/program.h" |
18 #include "src/vm/program_folder.h" | 18 #include "src/vm/program_folder.h" |
19 #include "src/vm/program_info_block.h" | 19 #include "src/vm/program_info_block.h" |
20 #include "src/vm/scheduler.h" | 20 #include "src/vm/scheduler.h" |
21 #include "src/vm/session.h" | 21 #include "src/vm/session.h" |
22 #include "src/vm/snapshot.h" | 22 #include "src/vm/snapshot.h" |
23 | 23 |
24 namespace fletch { | 24 namespace dartino { |
25 | 25 |
26 class PrintInterceptorImpl : public PrintInterceptor { | 26 class PrintInterceptorImpl : public PrintInterceptor { |
27 public: | 27 public: |
28 typedef void (*PrintFunction)(const char* message, int out, void* data); | 28 typedef void (*PrintFunction)(const char* message, int out, void* data); |
29 | 29 |
30 PrintInterceptorImpl(PrintFunction fn, void* data) : fn_(fn), data_(data) {} | 30 PrintInterceptorImpl(PrintFunction fn, void* data) : fn_(fn), data_(data) {} |
31 | 31 |
32 virtual void Out(char* message) { fn_(message, 2, data_); } | 32 virtual void Out(char* message) { fn_(message, 2, data_); } |
33 virtual void Error(char* message) { fn_(message, 3, data_); } | 33 virtual void Error(char* message) { fn_(message, 3, data_); } |
34 | 34 |
35 private: | 35 private: |
36 PrintFunction fn_; | 36 PrintFunction fn_; |
37 void* data_; | 37 void* data_; |
38 }; | 38 }; |
39 | 39 |
40 static bool IsSnapshot(List<uint8> snapshot) { | 40 static bool IsSnapshot(List<uint8> snapshot) { |
41 return snapshot.length() > 2 && snapshot[0] == 0xbe && snapshot[1] == 0xef; | 41 return snapshot.length() > 2 && snapshot[0] == 0xbe && snapshot[1] == 0xef; |
42 } | 42 } |
43 | 43 |
44 static Program* LoadSnapshot(List<uint8> bytes) { | 44 static Program* LoadSnapshot(List<uint8> bytes) { |
45 if (IsSnapshot(bytes)) { | 45 if (IsSnapshot(bytes)) { |
46 SnapshotReader reader(bytes); | 46 SnapshotReader reader(bytes); |
47 return reader.ReadProgram(); | 47 return reader.ReadProgram(); |
48 } | 48 } |
49 return NULL; | 49 return NULL; |
50 } | 50 } |
51 | 51 |
52 static int RunProgram(Program* program) { | 52 static int RunProgram(Program* program) { |
53 #ifdef FLETCH_ENABLE_LIVE_CODING | 53 #ifdef DARTINO_ENABLE_LIVE_CODING |
54 ProgramFolder::FoldProgramByDefault(program); | 54 ProgramFolder::FoldProgramByDefault(program); |
55 #endif // FLETCH_ENABLE_LIVE_CODING | 55 #endif // DARTINO_ENABLE_LIVE_CODING |
56 | 56 |
57 SimpleProgramRunner runner; | 57 SimpleProgramRunner runner; |
58 | 58 |
59 int exitcodes[1] = { -1 }; | 59 int exitcodes[1] = { -1 }; |
60 Program* programs[1] = { program }; | 60 Program* programs[1] = { program }; |
61 runner.Run(1, exitcodes, programs); | 61 runner.Run(1, exitcodes, programs); |
62 | 62 |
63 return exitcodes[0]; | 63 return exitcodes[0]; |
64 } | 64 } |
65 | 65 |
66 static void StartProgram(Program* program, | 66 static void StartProgram(Program* program, |
67 ProgramExitListener listener, | 67 ProgramExitListener listener, |
68 void* data) { | 68 void* data) { |
69 #ifdef FLETCH_ENABLE_LIVE_CODING | 69 #ifdef DARTINO_ENABLE_LIVE_CODING |
70 ProgramFolder::FoldProgramByDefault(program); | 70 ProgramFolder::FoldProgramByDefault(program); |
71 #endif // FLETCH_ENABLE_LIVE_CODING | 71 #endif // DARTINO_ENABLE_LIVE_CODING |
72 | 72 |
73 program->SetProgramExitListener(listener, data); | 73 program->SetProgramExitListener(listener, data); |
74 Process* process = program->ProcessSpawnForMain(); | 74 Process* process = program->ProcessSpawnForMain(); |
75 Scheduler::GlobalInstance()->ScheduleProgram(program, process); | 75 Scheduler::GlobalInstance()->ScheduleProgram(program, process); |
76 } | 76 } |
77 | 77 |
78 static Program* LoadSnapshotFromFile(const char* path) { | 78 static Program* LoadSnapshotFromFile(const char* path) { |
79 List<uint8> bytes = Platform::LoadFile(path); | 79 List<uint8> bytes = Platform::LoadFile(path); |
80 Program* program = LoadSnapshot(bytes); | 80 Program* program = LoadSnapshot(bytes); |
81 bytes.Delete(); | 81 bytes.Delete(); |
82 return program; | 82 return program; |
83 } | 83 } |
84 | 84 |
85 static void RunSnapshotFromFile(const char* path) { | 85 static void RunSnapshotFromFile(const char* path) { |
86 Program* program = LoadSnapshotFromFile(path); | 86 Program* program = LoadSnapshotFromFile(path); |
87 int result = RunProgram(program); | 87 int result = RunProgram(program); |
88 delete program; | 88 delete program; |
89 if (result != 0) FATAL1("Failed to run snapshot: %s\n", path); | 89 if (result != 0) FATAL1("Failed to run snapshot: %s\n", path); |
90 } | 90 } |
91 | 91 |
92 static void WaitForDebuggerConnection(int port) { | 92 static void WaitForDebuggerConnection(int port) { |
93 #ifdef FLETCH_ENABLE_LIVE_CODING | 93 #ifdef DARTINO_ENABLE_LIVE_CODING |
94 ConnectionListener listener("127.0.0.1", port); | 94 ConnectionListener listener("127.0.0.1", port); |
95 Connection* connection = listener.Accept(); | 95 Connection* connection = listener.Accept(); |
96 Session session(connection); | 96 Session session(connection); |
97 session.Initialize(); | 97 session.Initialize(); |
98 session.StartMessageProcessingThread(); | 98 session.StartMessageProcessingThread(); |
99 bool success = session.ProcessRun() == 0; | 99 bool success = session.ProcessRun() == 0; |
100 if (!success) FATAL("Failed to run via debugger connection"); | 100 if (!success) FATAL("Failed to run via debugger connection"); |
101 #else | 101 #else |
102 FATAL("fletch was built without live coding support."); | 102 FATAL("dartino was built without live coding support."); |
103 #endif | 103 #endif |
104 } | 104 } |
105 } // namespace fletch | 105 } // namespace dartino |
106 | 106 |
107 void FletchSetup() { fletch::Fletch::Setup(); } | 107 void DartinoSetup() { dartino::Dartino::Setup(); } |
108 | 108 |
109 void FletchTearDown() { fletch::Fletch::TearDown(); } | 109 void DartinoTearDown() { dartino::Dartino::TearDown(); } |
110 | 110 |
111 void FletchWaitForDebuggerConnection(int port) { | 111 void DartinoWaitForDebuggerConnection(int port) { |
112 fletch::WaitForDebuggerConnection(port); | 112 dartino::WaitForDebuggerConnection(port); |
113 } | 113 } |
114 | 114 |
115 FletchProgram FletchLoadSnapshotFromFile(const char* path) { | 115 DartinoProgram DartinoLoadSnapshotFromFile(const char* path) { |
116 fletch::Program* program = fletch::LoadSnapshotFromFile(path); | 116 dartino::Program* program = dartino::LoadSnapshotFromFile(path); |
117 if (program == NULL) FATAL("Failed to load snapshot from file.\n"); | 117 if (program == NULL) FATAL("Failed to load snapshot from file.\n"); |
118 return reinterpret_cast<FletchProgram>(program); | 118 return reinterpret_cast<DartinoProgram>(program); |
119 } | 119 } |
120 | 120 |
121 FletchProgram FletchLoadSnapshot(unsigned char* snapshot, int length) { | 121 DartinoProgram DartinoLoadSnapshot(unsigned char* snapshot, int length) { |
122 fletch::List<uint8> bytes(snapshot, length); | 122 dartino::List<uint8> bytes(snapshot, length); |
123 fletch::Program* program = fletch::LoadSnapshot(bytes); | 123 dartino::Program* program = dartino::LoadSnapshot(bytes); |
124 if (program == NULL) FATAL("Failed to load snapshot.\n"); | 124 if (program == NULL) FATAL("Failed to load snapshot.\n"); |
125 return reinterpret_cast<FletchProgram>(program); | 125 return reinterpret_cast<DartinoProgram>(program); |
126 } | 126 } |
127 | 127 |
128 int FletchRunMain(FletchProgram raw_program) { | 128 int DartinoRunMain(DartinoProgram raw_program) { |
129 fletch::Program* program = reinterpret_cast<fletch::Program*>(raw_program); | 129 dartino::Program* program = reinterpret_cast<dartino::Program*>(raw_program); |
130 return fletch::RunProgram(program); | 130 return dartino::RunProgram(program); |
131 } | 131 } |
132 | 132 |
133 void FletchRunMultipleMain(int count, | 133 void DartinoRunMultipleMain(int count, |
134 FletchProgram* fletch_programs, | 134 DartinoProgram* dartino_programs, |
135 int* exitcodes) { | 135 int* exitcodes) { |
136 fletch::SimpleProgramRunner runner; | 136 dartino::SimpleProgramRunner runner; |
137 | 137 |
138 auto programs = reinterpret_cast<fletch::Program**>(fletch_programs); | 138 auto programs = reinterpret_cast<dartino::Program**>(dartino_programs); |
139 for (int i = 0; i < count; i++) { | 139 for (int i = 0; i < count; i++) { |
140 exitcodes[i] = -1; | 140 exitcodes[i] = -1; |
141 #ifdef FLETCH_ENABLE_LIVE_CODING | 141 #ifdef DARTINO_ENABLE_LIVE_CODING |
142 fletch::ProgramFolder::FoldProgramByDefault(programs[i]); | 142 dartino::ProgramFolder::FoldProgramByDefault(programs[i]); |
143 #endif // FLETCH_ENABLE_LIVE_CODING | 143 #endif // DARTINO_ENABLE_LIVE_CODING |
144 } | 144 } |
145 | 145 |
146 runner.Run(count, exitcodes, programs); | 146 runner.Run(count, exitcodes, programs); |
147 } | 147 } |
148 | 148 |
149 FletchProgram FletchLoadProgramFromFlash(void* heap, size_t size) { | 149 DartinoProgram DartinoLoadProgramFromFlash(void* heap, size_t size) { |
150 fletch::Program* program = | 150 dartino::Program* program = |
151 new fletch::Program(fletch::Program::kLoadedFromSnapshot); | 151 new dartino::Program(dartino::Program::kLoadedFromSnapshot); |
152 uword address = reinterpret_cast<uword>(heap); | 152 uword address = reinterpret_cast<uword>(heap); |
153 // The info block is appended at the end of the image. | 153 // The info block is appended at the end of the image. |
154 size_t heap_size = size - sizeof(fletch::ProgramInfoBlock); | 154 size_t heap_size = size - sizeof(dartino::ProgramInfoBlock); |
155 uword block_address = address + heap_size; | 155 uword block_address = address + heap_size; |
156 fletch::ProgramInfoBlock* program_info = | 156 dartino::ProgramInfoBlock* program_info = |
157 reinterpret_cast<fletch::ProgramInfoBlock*>(block_address); | 157 reinterpret_cast<dartino::ProgramInfoBlock*>(block_address); |
158 program_info->WriteToProgram(program); | 158 program_info->WriteToProgram(program); |
159 fletch::Chunk* memory = fletch::ObjectMemory::CreateFlashChunk( | 159 dartino::Chunk* memory = dartino::ObjectMemory::CreateFlashChunk( |
160 program->heap()->space(), heap, heap_size); | 160 program->heap()->space(), heap, heap_size); |
161 | 161 |
162 program->heap()->space()->Append(memory); | 162 program->heap()->space()->Append(memory); |
163 program->heap()->space()->SetReadOnly(); | 163 program->heap()->space()->SetReadOnly(); |
164 return reinterpret_cast<FletchProgram>(program); | 164 return reinterpret_cast<DartinoProgram>(program); |
165 } | 165 } |
166 | 166 |
167 FLETCH_EXPORT void FletchStartMain(FletchProgram raw_program, | 167 DARTINO_EXPORT void DartinoStartMain(DartinoProgram raw_program, |
168 ProgramExitCallback callback, | 168 ProgramExitCallback callback, |
169 void* callback_data) { | 169 void* callback_data) { |
170 fletch::Program* program = reinterpret_cast<fletch::Program*>(raw_program); | 170 dartino::Program* program = reinterpret_cast<dartino::Program*>(raw_program); |
171 fletch::ProgramExitListener listener = | 171 dartino::ProgramExitListener listener = |
172 reinterpret_cast<fletch::ProgramExitListener>(callback); | 172 reinterpret_cast<dartino::ProgramExitListener>(callback); |
173 fletch::StartProgram(program, listener, callback_data); | 173 dartino::StartProgram(program, listener, callback_data); |
174 } | 174 } |
175 | 175 |
176 void FletchDeleteProgram(FletchProgram raw_program) { | 176 void DartinoDeleteProgram(DartinoProgram raw_program) { |
177 fletch::Program* program = reinterpret_cast<fletch::Program*>(raw_program); | 177 dartino::Program* program = reinterpret_cast<dartino::Program*>(raw_program); |
178 delete program; | 178 delete program; |
179 } | 179 } |
180 | 180 |
181 void FletchRunSnapshotFromFile(const char* path) { | 181 void DartinoRunSnapshotFromFile(const char* path) { |
182 fletch::RunSnapshotFromFile(path); | 182 dartino::RunSnapshotFromFile(path); |
183 } | 183 } |
184 | 184 |
185 bool FletchAddDefaultSharedLibrary(const char* library) { | 185 bool DartinoAddDefaultSharedLibrary(const char* library) { |
186 return fletch::ForeignFunctionInterface::AddDefaultSharedLibrary(library); | 186 return dartino::ForeignFunctionInterface::AddDefaultSharedLibrary(library); |
187 } | 187 } |
188 | 188 |
189 FletchPrintInterceptor FletchRegisterPrintInterceptor( | 189 DartinoPrintInterceptor DartinoRegisterPrintInterceptor( |
190 PrintInterceptionFunction function, void* data) { | 190 PrintInterceptionFunction function, void* data) { |
191 fletch::PrintInterceptorImpl* impl = | 191 dartino::PrintInterceptorImpl* impl = |
192 new fletch::PrintInterceptorImpl(function, data); | 192 new dartino::PrintInterceptorImpl(function, data); |
193 fletch::Print::RegisterPrintInterceptor(impl); | 193 dartino::Print::RegisterPrintInterceptor(impl); |
194 return reinterpret_cast<void*>(impl); | 194 return reinterpret_cast<void*>(impl); |
195 } | 195 } |
196 | 196 |
197 void FletchUnregisterPrintInterceptor(FletchPrintInterceptor raw_interceptor) { | 197 void DartinoUnregisterPrintInterceptor( |
198 fletch::PrintInterceptorImpl* impl = | 198 DartinoPrintInterceptor raw_interceptor) { |
199 reinterpret_cast<fletch::PrintInterceptorImpl*>(raw_interceptor); | 199 dartino::PrintInterceptorImpl* impl = |
200 fletch::Print::UnregisterPrintInterceptor(impl); | 200 reinterpret_cast<dartino::PrintInterceptorImpl*>(raw_interceptor); |
| 201 dartino::Print::UnregisterPrintInterceptor(impl); |
201 delete impl; | 202 delete impl; |
202 } | 203 } |
OLD | NEW |