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

Return mtime from get_contents as well

Lucas Stadler лет назад: 9
Родитель
Сommit
b386294fdc
1 измененных файлов с 19 добавлено и 14 удалено
  1. 19 14
      c/ton/main.c

+ 19 - 14
c/ton/main.c

6
#include <stdlib.h>
6
#include <stdlib.h>
7
#include <string.h>
7
#include <string.h>
8
#include <sys/stat.h>
8
#include <sys/stat.h>
9
#include <time.h>
9
10
10
#include <JavaScriptCore/JavaScript.h>
11
#include <JavaScriptCore/JavaScript.h>
11
12
27
void bootstrap(JSContextRef ctx, char *deps_file_path, char *goog_base_path);
28
void bootstrap(JSContextRef ctx, char *deps_file_path, char *goog_base_path);
28
JSObjectRef get_function(JSContextRef ctx, char *namespace, char *name);
29
JSObjectRef get_function(JSContextRef ctx, char *namespace, char *name);
29
30
30
char* get_contents(char *path);
31
char* get_contents(char *path, time_t *last_modified);
31
void write_contents(char *path, char *contents);
32
void write_contents(char *path, char *contents);
32
int mkdir_p(char *path);
33
int mkdir_p(char *path);
33
34
97
		// TODO: should not load from here?
98
		// TODO: should not load from here?
98
		snprintf(full_path, 150, "%s/%s", "out", path);
99
		snprintf(full_path, 150, "%s/%s", "out", path);
99
100
100
		char *contents = get_contents(full_path);
101
		time_t last_modified = 0;
102
		char *contents = get_contents(full_path, &last_modified);
101
		if (contents != NULL) {
103
		if (contents != NULL) {
102
			JSStringRef contents_str = JSStringCreateWithUTF8CString(contents);
104
			JSStringRef contents_str = JSStringCreateWithUTF8CString(contents);
103
			free(contents);
105
			free(contents);
104
106
105
			JSValueRef res[2];
107
			JSValueRef res[2];
106
			res[0] = JSValueMakeString(ctx, contents_str);
108
			res[0] = JSValueMakeString(ctx, contents_str);
107
			res[1] = JSValueMakeNumber(ctx, 0);
109
			res[1] = JSValueMakeNumber(ctx, last_modified);
108
			return JSObjectMakeArray(ctx, 2, res, NULL);
110
			return JSObjectMakeArray(ctx, 2, res, NULL);
109
		}
111
		}
110
	}
112
	}
131
133
132
		JSValueRef contents_val = NULL;
134
		JSValueRef contents_val = NULL;
133
135
134
		char *contents = get_contents(full_path);
136
		time_t last_modified = 0;
137
		char *contents = get_contents(full_path, &last_modified);
135
		if (contents != NULL) {
138
		if (contents != NULL) {
136
			JSStringRef contents_str = JSStringCreateWithUTF8CString(contents);
139
			JSStringRef contents_str = JSStringCreateWithUTF8CString(contents);
137
			free(contents);
140
			free(contents);
138
141
139
			contents_val = JSValueMakeString(ctx, contents_str);
142
			JSValueRef res[2];
143
			res[0] = JSValueMakeString(ctx, contents_str);;
144
			res[1] = JSValueMakeNumber(ctx, last_modified);
145
			return JSObjectMakeArray(ctx, 2, res, NULL);
140
		}
146
		}
141
142
		JSValueRef res[2];
143
		res[0] = contents_val;
144
		res[1] = JSValueMakeNumber(ctx, 0);
145
		return JSObjectMakeArray(ctx, 2, res, NULL);
146
	}
147
	}
147
148
148
	return JSValueMakeNull(ctx);
149
	return JSValueMakeNull(ctx);
272
273
273
		char full_path[150];
274
		char full_path[150];
274
		snprintf(full_path, 150, "%s/%s", "out", path);
275
		snprintf(full_path, 150, "%s/%s", "out", path);
275
		char *buf = get_contents(full_path);
276
		char *buf = get_contents(full_path, NULL);
276
		if (buf == NULL) {
277
		if (buf == NULL) {
277
			goto err;
278
			goto err;
278
		}
279
		}
653
	evaluate_script(ctx, "CLOSURE_IMPORT_SCRIPT = function(src) { IMPORT_SCRIPT('goog/' + src); return true; }", source);
654
	evaluate_script(ctx, "CLOSURE_IMPORT_SCRIPT = function(src) { IMPORT_SCRIPT('goog/' + src); return true; }", source);
654
655
655
	// Load goog base
656
	// Load goog base
656
	char *base_script_str = get_contents(goog_base_path);
657
	char *base_script_str = get_contents(goog_base_path, NULL);
657
	if (base_script_str == NULL) {
658
	if (base_script_str == NULL) {
658
		fprintf(stderr, "The goog base JavaScript text could not be loaded");
659
		fprintf(stderr, "The goog base JavaScript text could not be loaded");
659
		exit(1);
660
		exit(1);
662
	free(base_script_str);
663
	free(base_script_str);
663
664
664
	// Load the deps file
665
	// Load the deps file
665
	char *deps_script_str = get_contents(deps_file_path);
666
	char *deps_script_str = get_contents(deps_file_path, NULL);
666
	if (deps_script_str == NULL) {
667
	if (deps_script_str == NULL) {
667
		fprintf(stderr, "The goog base JavaScript text could not be loaded");
668
		fprintf(stderr, "The goog base JavaScript text could not be loaded");
668
		exit(1);
669
		exit(1);
690
			"};", source);
691
			"};", source);
691
}
692
}
692
693
693
char *get_contents(char *path) {
694
char *get_contents(char *path, time_t *last_modified) {
694
/*#ifdef DEBUG
695
/*#ifdef DEBUG
695
	printf("get_contents(\"%s\")\n", path);
696
	printf("get_contents(\"%s\")\n", path);
696
#endif*/
697
#endif*/
709
		goto err;
710
		goto err;
710
	}
711
	}
711
712
713
	if (last_modified != NULL) {
714
		*last_modified = f_stat.st_mtim.tv_sec;
715
	}
716
712
	char *buf = malloc(f_stat.st_size + 1);
717
	char *buf = malloc(f_stat.st_size + 1);
713
	memset(buf, 0, f_stat.st_size);
718
	memset(buf, 0, f_stat.st_size);
714
	fread(buf, f_stat.st_size, 1, f);
719
	fread(buf, f_stat.st_size, 1, f);