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