Skip to content

Commit f05dd21

Browse files
committed
make --run work, modulu exit codes
it will not exit with EXIT_ERROR_CHECK_FAILURES when needed. however, it the main function in pyret.arr will return an appropriate error code, if we can leverage that also: this commit fails on no-diff-standalone
1 parent 77d468c commit f05dd21

File tree

5 files changed

+120
-83
lines changed

5 files changed

+120
-83
lines changed

src/arr/compiler/cli-module-loader.arr

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -307,13 +307,30 @@ fun compile(path, options):
307307
compiled
308308
end
309309

310+
fun handle-compilation-errors(problems, options) block:
311+
for lists.each(e from problems) block:
312+
options.log-error(RED.display-to-string(e.render-reason(), torepr, empty))
313+
options.log-error("\n")
314+
end
315+
#raise("There were compilation errors")
316+
{
317+
message: "There were compilation errors",
318+
exit-code: 1
319+
}
320+
end
321+
310322
fun run(path, options):
311-
prog = build-program(path, options)
312-
result = L.run-program(R.make-runtime(), L.empty-realm(), prog.js-ast.to-ugly-source(), options)
313-
if L.is-success-result(result):
314-
print(L.render-check-results(result))
315-
else:
316-
print(L.render-error-message(result))
323+
maybe-program = build-program(path, options)
324+
cases(Either) maybe-program block:
325+
| left(problems) =>
326+
handle-compilation-errors(problems, options)
327+
| right(program) =>
328+
result = L.run-program(R.make-runtime(), L.empty-realm(), program.js-ast.to-ugly-source(), options)
329+
if L.is-success-result(result):
330+
L.render-check-results(result)
331+
else:
332+
L.render-error-message(result)
333+
end
317334
end
318335
end
319336

@@ -384,11 +401,7 @@ fun build-runnable-standalone(path, require-config-path, outfile, options) block
384401
maybe-program = build-program(path, options)
385402
cases(Either) maybe-program block:
386403
| left(problems) =>
387-
for lists.each(e from problems) block:
388-
options.log-error(RED.display-to-string(e.render-reason(), torepr, empty))
389-
options.log-error("\n")
390-
end
391-
raise("There were compilation errors")
404+
handle-compilation-errors(problems, options)
392405
| right(program) =>
393406
config = JSON.read-json(F.file-to-string(require-config-path)).dict.unfreeze()
394407
config.set-now("out", JSON.j-str(outfile))

src/arr/compiler/pyret.arr

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,22 @@ fun main(args):
6767

6868
cases(C.ParsedArguments) params-parsed block:
6969
| success(r, rest) =>
70-
check-mode = not(r.has-key("no-check-mode") or r.has-key("library"))
71-
allow-shadowed = r.has-key("allow-shadow")
7270
libs =
7371
if r.has-key("library"): CS.minimal-imports
7472
else: CS.standard-imports end
7573
module-dir = r.get-value("module-load-dir")
76-
check-all = r.has-key("check-all")
77-
type-check = r.has-key("type-check")
78-
tail-calls = not(r.has-key("improper-tail-calls"))
79-
compiled-dir = r.get-value("compiled-dir")
80-
standalone-file = r.get-value("standalone-file")
81-
display-progress = not(r.has-key("no-display-progress"))
74+
user-compile-options = CS.default-compile-options.{
75+
check-mode: not(r.has-key("no-check-mode") or r.has-key("library")),
76+
allow-shadowed: r.has-key("allow-shadow"),
77+
check-all: r.has-key("check-all"),
78+
type-check: r.has-key("type-check"),
79+
tail-calls: not(r.has-key("improper-tail-calls")),
80+
compiled-dir: r.get-value("compiled-dir"),
81+
standalone-file: r.get-value("standalone-file"),
82+
display-progress: not(r.has-key("no-display-progress")),
83+
collect-all: false,
84+
ignore-unbound: false
85+
}
8286
when r.has-key("builtin-js-dir"):
8387
B.set-builtin-js-dirs(r.get-value("builtin-js-dir"))
8488
end
@@ -101,17 +105,8 @@ fun main(args):
101105
r.get-value("build-runnable"),
102106
r.get-value("require-config"),
103107
outfile,
104-
CS.default-compile-options.{
105-
standalone-file: standalone-file,
106-
check-mode : check-mode,
107-
type-check : type-check,
108-
allow-shadowed : allow-shadowed,
109-
collect-all: false,
110-
ignore-unbound: false,
111-
proper-tail-calls: tail-calls,
112-
compile-module: true,
113-
compiled-cache: compiled-dir,
114-
display-progress: display-progress
108+
user-compile-options.{
109+
compile-module: true
115110
})
116111
else if r.has-key("serve"):
117112
port = r.get-value("port")
@@ -134,15 +129,8 @@ fun main(args):
134129
|#
135130
else if r.has-key("build"):
136131
result = CLI.compile(r.get-value("build"),
137-
CS.default-compile-options.{
138-
check-mode : check-mode,
139-
type-check : type-check,
140-
allow-shadowed : allow-shadowed,
141-
collect-all: false,
142-
ignore-unbound: false,
143-
proper-tail-calls: tail-calls,
144-
compile-module: false,
145-
display-progress: display-progress
132+
user-compile-options.{
133+
compile-module: false
146134
})
147135
failures = filter(CS.is-err, result.loadables)
148136
when is-link(failures):
@@ -155,12 +143,14 @@ fun main(args):
155143
end
156144
end
157145
else if r.has-key("run"):
158-
CLI.run(r.get-value("run"), CS.default-compile-options.{
159-
standalone-file: standalone-file,
160-
compile-module: true,
161-
display-progress: display-progress,
162-
check-all: check-all
163-
})
146+
block:
147+
result = CLI.run(r.get-value("run"), user-compile-options.{
148+
compile-module: false
149+
})
150+
print(result.message)
151+
print("\n")
152+
result.exit-code # TODO: exit process with this code
153+
end
164154
else:
165155
print(C.usage-info(options).join-str("\n"))
166156
raise("Unknown command line options")

src/js/base/handalone.js

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ require(["pyret-base/js/runtime", "program"], function(runtimeLib, program) {
2020
var EXIT_ERROR_JS = 5;
2121
var EXIT_ERROR_UNKNOWN = 6;
2222

23-
runtime.setParam("command-line-arguments", process.argv.slice(1));
23+
var commandLineArguments = process.argv.slice(1);
24+
var runMode = (commandLineArguments.indexOf("--run") > 0);
25+
26+
runtime.setParam("command-line-arguments", commandLineArguments);
2427

2528
var postLoadHooks = {
2629
"builtin://srcloc": function(srcloc) {
@@ -147,35 +150,39 @@ require(["pyret-base/js/runtime", "program"], function(runtimeLib, program) {
147150
runtime.setParam("current-checker", currentChecker);
148151
}
149152
};
150-
postLoadHooks[main] = function(answer) {
151-
var checkerLib = runtime.modules["builtin://checker"];
152-
var checker = runtime.getField(runtime.getField(checkerLib, "provide-plus-types"), "values");
153-
var getStack = function(err) {
154-
console.error("The error is: ", err);
155-
var locArray = err.val.pyretStack.map(runtime.makeSrcloc);
156-
var locList = runtime.ffi.makeList(locArray);
157-
return locList;
158-
};
159-
var getStackP = runtime.makeFunction(getStack, "get-stack");
160-
var toCall = runtime.getField(checker, "render-check-results-stack");
161-
var checks = runtime.getField(answer, "checks");
162-
runtime.safeCall(function() {
163-
return toCall.app(checks, getStackP);
164-
}, function(summary) {
165-
if(runtime.isObject(summary)) {
166-
process.stdout.write(runtime.getField(summary, "message"));
167-
process.stdout.write("\n");
168-
var errs = runtime.getField(summary, "errored");
169-
var failed = runtime.getField(summary, "failed");
170-
if(errs !== 0 || failed !== 0) {
171-
process.exit(EXIT_ERROR_CHECK_FAILURES);
172-
}
173-
else {
174-
process.exit(EXIT_SUCCESS);
175-
}
176-
}
177-
});
178-
}
153+
postLoadHooks[main] = runMode ?
154+
(function(answer) {
155+
//console.dir(answer);
156+
}) :
157+
(function(answer) {
158+
var checkerLib = runtime.modules["builtin://checker"];
159+
var checker = runtime.getField(runtime.getField(checkerLib, "provide-plus-types"), "values");
160+
var getStack = function(err) {
161+
console.error("The error is: ", err);
162+
var locArray = err.val.pyretStack.map(runtime.makeSrcloc);
163+
var locList = runtime.ffi.makeList(locArray);
164+
return locList;
165+
};
166+
var getStackP = runtime.makeFunction(getStack, "get-stack");
167+
var toCall = runtime.getField(checker, "render-check-results-stack");
168+
var checks = runtime.getField(answer, "checks");
169+
runtime.safeCall(function() {
170+
return toCall.app(checks, getStackP);
171+
}, function(summary) {
172+
if(runtime.isObject(summary)) {
173+
process.stdout.write(runtime.getField(summary, "message"));
174+
process.stdout.write("\n");
175+
var errs = runtime.getField(summary, "errored");
176+
var failed = runtime.getField(summary, "failed");
177+
if(errs !== 0 || failed !== 0) {
178+
process.exit(EXIT_ERROR_CHECK_FAILURES);
179+
}
180+
else {
181+
process.exit(EXIT_SUCCESS);
182+
}
183+
}
184+
});
185+
});
179186

180187
function renderErrorMessageAndExit(execRt, res) {
181188
if (execRt.isPyretException(res.exn)) {

src/js/base/runtime.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4901,7 +4901,7 @@ function (Namespace, jsnums, codePoint, seedrandom, util) {
49014901
return mod.theModule.apply(null, [thisRuntime, thisRuntime.namespace, uri].concat(reqInstantiated).concat(natives));
49024902
},
49034903
function(r) {
4904-
// CONSOLE.log("Result from module: ", r);
4904+
//CONSOLE.log("Result from module: ", r);
49054905
realm[uri] = r;
49064906
if(uri in postLoadHooks) {
49074907
return thisRuntime.safeCall(function() {

src/js/trove/load-lib.js

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
nativeRequires: ["pyret-base/js/secure-loader"],
66
provides: {},
77
theModule: function(runtime, namespace, uri, runtimeLib, loader) {
8+
var EXIT_SUCCESS = 0;
9+
var EXIT_ERROR = 1;
10+
var EXIT_ERROR_RENDERING_ERROR = 2;
11+
var EXIT_ERROR_DISPLAYING_ERROR = 3;
12+
var EXIT_ERROR_CHECK_FAILURES = 4;
13+
var EXIT_ERROR_JS = 5;
14+
var EXIT_ERROR_UNKNOWN = 6;
815

916

1017
var brandModule = runtime.namedBrander("module", ["load-lib: module brander"]);
@@ -140,16 +147,36 @@
140147
var getStackP = execRt.makeFunction(getStack, "get-stack");
141148
var checks = getModuleResultChecks(mr);
142149
execRt.runThunk(function() { return toCall.app(checks, getStackP); },
143-
function(printedCheckResult) {
144-
if(execRt.isSuccessResult(printedCheckResult)) {
145-
if(execRt.isString(printedCheckResult.result)) {
146-
restarter.resume(runtime.makeString(execRt.unwrap(printedCheckResult.result)));
150+
function(renderedCheckResults) {
151+
var resumeWith = {
152+
message: "Unknown error!",
153+
'exit-code': EXIT_ERROR_UNKNOWN
154+
};
155+
156+
if(execRt.isSuccessResult(renderedCheckResults)) {
157+
resumeWith.message = execRt.getField(renderedCheckResults.result, "message");
158+
var errs = execRt.getField(renderedCheckResults.result, "errored");
159+
var failed = execRt.getField(renderedCheckResults.result, "failed");
160+
if(errs !== 0 || failed !== 0) {
161+
resumeWith["exit-code"] = EXIT_ERROR_CHECK_FAILURES;
162+
} else {
163+
resumeWith["exit-code"] = EXIT_SUCCESS;
147164
}
148165
}
149-
else if(execRt.isFailureResult(printedCheckResult)) {
150-
console.error(printedCheckResult.exn.dict);
151-
restarter.resume(runtime.makeString("There was an exception while formatting the check results"));
166+
else if(execRt.isFailureResult(renderedCheckResults)) {
167+
resumeWith.message = execRt.makeString("There was an exception while formatting the check results");
168+
resumeWith["exit-code"] = EXIT_ERROR_RENDERING_ERROR;
169+
//console.error(renderedCheckResults.exn.dict);
152170
}
171+
172+
// process.stdout.write(resumeWith.message);
173+
// process.stdout.write("\n");
174+
// process.exit(resumeWith["exit-code"]);
175+
176+
restarter.resume(runtime.makeObject({
177+
message: runtime.makeString(resumeWith.message),
178+
'exit-code': runtime.makeNumber(resumeWith["exit-code"])
179+
}));
153180
});
154181
});
155182
}

0 commit comments

Comments
 (0)