ref: d800b59f0f0fabc15f3eb572ab85baefc0a7edef
parent: c1c637b7396e9cfe9227eb3fc06b61ec4fd6db83
author: Tor Andersson <tor.andersson@artifex.com>
date: Wed Jan 6 08:43:59 EST 2016
Clean up stack on errors in js_pcall and js_pconstruct. Exactly one value will remain on the stack after js_pcall: either the return value or the error object.
--- a/jsrun.c
+++ b/jsrun.c
@@ -1100,8 +1100,13 @@
int js_pconstruct(js_State *J, int n)
{
- if (js_try(J))
+ int savetop = TOP - n - 2;
+ if (js_try(J)) {
+ /* clean up the stack to only hold the error object */
+ STACK[savetop] = STACK[TOP-1];
+ TOP = savetop + 1;
return 1;
+ }
js_construct(J, n);
js_endtry(J);
return 0;
@@ -1109,8 +1114,13 @@
int js_pcall(js_State *J, int n)
{
- if (js_try(J))
+ int savetop = TOP - n - 2;
+ if (js_try(J)) {
+ /* clean up the stack to only hold the error object */
+ STACK[savetop] = STACK[TOP-1];
+ TOP = savetop + 1;
return 1;
+ }
js_call(J, n);
js_endtry(J);
return 0;
--- a/main.c
+++ b/main.c
@@ -123,11 +123,13 @@
{
if (js_ploadstring(J, "[string]", source)) {
fprintf(stderr, "%s\n", js_tostring(J, -1));
+ js_pop(J, 1);
return 1;
}
js_pushglobal(J);
if (js_pcall(J, 0)) {
fprintf(stderr, "%s\n", js_tostring(J, -1));
+ js_pop(J, 1);
return 1;
}
if (js_isdefined(J, -1))