Преглед на файлове

Port bootstrap from planck

See https://github.com/mfikes/planck the original implementation.

This is a work in progress, not sure what works, but the bootstrapping
"works" in that it can load the code from planck-cljs.
Lucas Stadler преди 9 години
родител
ревизия
688c69ed9c
променени са 1 файла, в които са добавени 49 реда и са изтрити 0 реда
  1. 49 0
      c/jsc-test.c

+ 49 - 0
c/jsc-test.c

@ -11,6 +11,8 @@ char console_log_buf[CONSOLE_LOG_BUF_SIZE];
11 11
JSStringRef to_string(JSContextRef ctx, JSValueRef val);
12 12
JSValueRef evaluate_script(JSContextRef ctx, char *script, char *source);
13 13
14
void bootstrap(JSContextRef ctx, char *deps_file_path, char *goog_base_path);
15
14 16
char* get_contents(char *path);
15 17
16 18
JSValueRef function_console_log(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) {
@ -95,6 +97,8 @@ int main(int argc, char **argv) {
95 97
			"console.log = CONSOLE_LOG;"\
96 98
			"console.error = CONSOLE_ERROR;", "<init>");
97 99
100
	bootstrap(ctx, "out/main.js", "out/goog/base.js");
101
98 102
	char *script;
99 103
	if (argc == 0) {
100 104
		script = "CONSOLE_LOG(\"Hello, World!\");";
@ -142,6 +146,7 @@ JSValueRef evaluate_script(JSContextRef ctx, char *script, char *source) {
142 146
143 147
#ifdef DEBUG
144 148
	if (ex != NULL) {
149
		printf("\n---\n%s\n---\n", script);
145 150
		JSStringRef ex_str = to_string(ctx, ex);
146 151
		char ex_buf[1000];
147 152
		ex_buf[0] = '\0';
@ -154,6 +159,50 @@ JSValueRef evaluate_script(JSContextRef ctx, char *script, char *source) {
154 159
	return val;
155 160
}
156 161
162
void bootstrap(JSContextRef ctx, char *deps_file_path, char *goog_base_path) {
163
	char source[] = "<bootstrap>";
164
165
	// Setup CLOSURE_IMPORT_SCRIPT
166
	evaluate_script(ctx, "CLOSURE_IMPORT_SCRIPT = function(src) { IMPORT_SCRIPT('goog/' + src); return true; }", source);
167
168
	// Load goog base
169
	char *base_script_str = get_contents(goog_base_path);
170
	if (base_script_str == NULL) {
171
		fprintf(stderr, "The goog base JavaScript text could not be loaded");
172
		exit(1);
173
	}
174
	evaluate_script(ctx, base_script_str, "<bootstrap:base>");
175
	free(base_script_str);
176
177
	// Load the deps file
178
	char *deps_script_str = get_contents(deps_file_path);
179
	if (deps_script_str == NULL) {
180
		fprintf(stderr, "The goog base JavaScript text could not be loaded");
181
		exit(1);
182
	}
183
	evaluate_script(ctx, deps_script_str, "<bootstrap:deps>");
184
	free(deps_script_str);
185
186
	evaluate_script(ctx, "goog.isProvided_ = function(x) { return false; };", source);
187
188
	evaluate_script(ctx, "goog.require = function (name) { return CLOSURE_IMPORT_SCRIPT(goog.dependencies_.nameToPath[name]); };", source);
189
190
	evaluate_script(ctx, "goog.require('cljs.core');", source);
191
192
	// redef goog.require to track loaded libs
193
	evaluate_script(ctx, "cljs.core._STAR_loaded_libs_STAR_ = cljs.core.into.call(null, cljs.core.PersistentHashSet.EMPTY, [\"cljs.core\"]);\n"
194
			"goog.require = function (name, reload) {\n"
195
			"    if(!cljs.core.contains_QMARK_(cljs.core._STAR_loaded_libs_STAR_, name) || reload) {\n"
196
			"        var AMBLY_TMP = cljs.core.PersistentHashSet.EMPTY;\n"
197
			"        if (cljs.core._STAR_loaded_libs_STAR_) {\n"
198
			"            AMBLY_TMP = cljs.core._STAR_loaded_libs_STAR_;\n"
199
			"        }\n"
200
			"        cljs.core._STAR_loaded_libs_STAR_ = cljs.core.into.call(null, AMBLY_TMP, [name]);\n"
201
			"        CLOSURE_IMPORT_SCRIPT(goog.dependencies_.nameToPath[name]);\n"
202
			"    }\n"
203
			"};", source);
204
}
205
157 206
char *get_contents(char *path) {
158 207
	FILE *f = fopen(path, "r");
159 208
	if (f == NULL) {