Skip to content

Commit 5b51342

Browse files
committed
Added js.source method
A method to evaluate other source files.
1 parent 2d70115 commit 5b51342

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

bin/buildnum

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
declare -i MAJOR="0"
44
declare -i MINOR="2"
55
declare -i MINORMAX="99"
6-
declare -i REVISION="506"
6+
declare -i REVISION="509"
77
declare -i REVISIONMAX="999"
88

99
buildnum ()

src/JSTools.c

+20
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,25 @@ static JSTValue jsToolsEnvUser JSTDeclareFunction () {
137137
return JSTValueUndefined;
138138
}
139139

140+
static JSValueRef jsToolsSource JSTDeclareFunction (file, [global object]) {
141+
JSTValue result;
142+
char *script = NULL, *src = NULL, *file = (argc)?JSTValueToUTF8(argv[0]):NULL;
143+
if (g_file_get_contents(file, &src, NULL, NULL)) {
144+
script = src; int c = 0;
145+
if (*script == '#' && *(script+1) == '!') {
146+
script+=2;
147+
while ((c = *script) && c != '\n') script++;
148+
}
149+
result = JSTScriptEval(script, (argc == 2)?(JSTObject)argv[1]:NULL, file, 1); g_free(src);
150+
} else {
151+
JSTScriptNativeError("unable to get file `%s' contents", file);
152+
}
153+
JSTStringFreeUTF8(file);
154+
return result;
155+
156+
}
157+
158+
140159
JSTObject JSTInit_ JSTUtility(JSTObject global, int argc, char * argv[], char * envp[]) {
141160

142161
static bool initialized;
@@ -148,6 +167,7 @@ JSTObject JSTInit_ JSTUtility(JSTObject global, int argc, char * argv[], char *
148167
if (JSTScriptHasError) JSTScriptReportException(), exit(1);
149168

150169
JSTObjectSetProperty(global, "js", js, JSTObjectPropertyReadOnly | JSTObjectPropertyRequired);
170+
JSTObjectSetMethod(js, "source", jsToolsSource, 0);
151171

152172
JSTObjectSetProperty(js, "user", jsToolsPasswdToObject(ctx, getuid(), exception), JSTObjectPropertyRequired);
153173

0 commit comments

Comments
 (0)