Просмотр исходного кода

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
Родитель
Сommit
688c69ed9c
1 измененных файлов с 49 добавлено и 0 удалено
  1. 49 0
      c/jsc-test.c

+ 49 - 0
c/jsc-test.c

11
JSStringRef to_string(JSContextRef ctx, JSValueRef val);
11
JSStringRef to_string(JSContextRef ctx, JSValueRef val);
12
JSValueRef evaluate_script(JSContextRef ctx, char *script, char *source);
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
char* get_contents(char *path);
16
char* get_contents(char *path);
15
17
16
JSValueRef function_console_log(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) {
18
JSValueRef function_console_log(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) {
95
			"console.log = CONSOLE_LOG;"\
97
			"console.log = CONSOLE_LOG;"\
96
			"console.error = CONSOLE_ERROR;", "<init>");
98
			"console.error = CONSOLE_ERROR;", "<init>");
97
99
100
	bootstrap(ctx, "out/main.js", "out/goog/base.js");
101
98
	char *script;
102
	char *script;
99
	if (argc == 0) {
103
	if (argc == 0) {
100
		script = "CONSOLE_LOG(\"Hello, World!\");";
104
		script = "CONSOLE_LOG(\"Hello, World!\");";
142
146
143
#ifdef DEBUG
147
#ifdef DEBUG
144
	if (ex != NULL) {
148
	if (ex != NULL) {
149
		printf("\n---\n%s\n---\n", script);
145
		JSStringRef ex_str = to_string(ctx, ex);
150
		JSStringRef ex_str = to_string(ctx, ex);
146
		char ex_buf[1000];
151
		char ex_buf[1000];
147
		ex_buf[0] = '\0';
152
		ex_buf[0] = '\0';
154
	return val;
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
char *get_contents(char *path) {
206
char *get_contents(char *path) {
158
	FILE *f = fopen(path, "r");
207
	FILE *f = fopen(path, "r");
159
	if (f == NULL) {
208
	if (f == NULL) {