From de17d45afaf1786a292869928cb13b9eda22ef04 Mon Sep 17 00:00:00 2001 From: "rogerl%netscape.com" Date: Fri, 28 Apr 2000 00:11:18 +0000 Subject: [PATCH] try/catch exercise changes git-svn-id: svn://10.0.0.236/trunk@67445 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/js/js2/js2.cpp | 42 ++++++++++++++++++++++++++--- mozilla/js2/tests/cpp/js2_shell.cpp | 42 ++++++++++++++++++++++++++--- 2 files changed, 76 insertions(+), 8 deletions(-) diff --git a/mozilla/js/js2/js2.cpp b/mozilla/js/js2/js2.cpp index 8b1f6f8c270..3d80c55a760 100644 --- a/mozilla/js/js2/js2.cpp +++ b/mozilla/js/js2/js2.cpp @@ -122,19 +122,20 @@ static void readEvalPrint(FILE *in, World &world) } stdOut << '\n'; } - + static void testICG(World &world) { // // testing ICG // uint32 pos = 0; - ICodeGenerator icg; + ICodeGenerator icg(&world, true, 1); // var i,j; // i is bound to var #0, j to var #1 Register r_i = icg.allocateVariable(world.identifiers[widenCString("i")]); Register r_j = icg.allocateVariable(world.identifiers[widenCString("j")]); + Register r_x = icg.allocateVariable(world.identifiers[widenCString("x")]); // i = j + 2; icg.beginStatement(pos); @@ -171,6 +172,39 @@ static void testICG(World &world) icg.beginElseStatement(false); icg.endIfStatement(); + // try { + // if (i) if (j) i = 3; else j = 4; + // throw j; + // } + // catch (x) { + // j = x; + // } + // finally { + // i = 5; + // } + icg.beginTryStatement(pos, true, true); // hasCatch, hasFinally + icg.beginIfStatement(pos, r_i); + icg.beginIfStatement(pos, r_j); + icg.move(r_i, icg.loadImmediate(3)); + icg.beginElseStatement(true); + icg.beginStatement(pos); + icg.move(r_j, icg.loadImmediate(4)); + icg.endIfStatement(); + icg.beginElseStatement(false); + icg.endIfStatement(); + icg.throwStatement(pos, r_j); + icg.endTryBlock(); + icg.beginCatchStatement(pos); + icg.endCatchExpression(r_x); + icg.beginStatement(pos); + icg.move(r_j, r_x); + icg.endCatchStatement(); + icg.beginFinallyStatement(pos); + icg.beginStatement(pos); + icg.move(r_i, icg.loadImmediate(5)); + icg.endFinallyStatement(); + icg.endTryStatement(); + // switch (i) { case 3: case 4: j = 4; break; case 5: j = 5; break; default : j = 6; } icg.beginSwitchStatement(pos, r_i); @@ -419,8 +453,8 @@ int main(int argc, char **argv) #if 1 assert(JavaScript::Shell::testFactorial(world, 5) == 120); assert(JavaScript::Shell::testObjects(world, 5) == 5); - // JavaScript::Shell::testICG(world); - assert(JavaScript::Shell::testFunctionCall(world, 5) == 5); +// JavaScript::Shell::testICG(world); + assert(JavaScript::Shell::testFunctionCall(world, 5) == 5); #endif JavaScript::Shell::readEvalPrint(stdin, world); return 0; diff --git a/mozilla/js2/tests/cpp/js2_shell.cpp b/mozilla/js2/tests/cpp/js2_shell.cpp index 8b1f6f8c270..3d80c55a760 100644 --- a/mozilla/js2/tests/cpp/js2_shell.cpp +++ b/mozilla/js2/tests/cpp/js2_shell.cpp @@ -122,19 +122,20 @@ static void readEvalPrint(FILE *in, World &world) } stdOut << '\n'; } - + static void testICG(World &world) { // // testing ICG // uint32 pos = 0; - ICodeGenerator icg; + ICodeGenerator icg(&world, true, 1); // var i,j; // i is bound to var #0, j to var #1 Register r_i = icg.allocateVariable(world.identifiers[widenCString("i")]); Register r_j = icg.allocateVariable(world.identifiers[widenCString("j")]); + Register r_x = icg.allocateVariable(world.identifiers[widenCString("x")]); // i = j + 2; icg.beginStatement(pos); @@ -171,6 +172,39 @@ static void testICG(World &world) icg.beginElseStatement(false); icg.endIfStatement(); + // try { + // if (i) if (j) i = 3; else j = 4; + // throw j; + // } + // catch (x) { + // j = x; + // } + // finally { + // i = 5; + // } + icg.beginTryStatement(pos, true, true); // hasCatch, hasFinally + icg.beginIfStatement(pos, r_i); + icg.beginIfStatement(pos, r_j); + icg.move(r_i, icg.loadImmediate(3)); + icg.beginElseStatement(true); + icg.beginStatement(pos); + icg.move(r_j, icg.loadImmediate(4)); + icg.endIfStatement(); + icg.beginElseStatement(false); + icg.endIfStatement(); + icg.throwStatement(pos, r_j); + icg.endTryBlock(); + icg.beginCatchStatement(pos); + icg.endCatchExpression(r_x); + icg.beginStatement(pos); + icg.move(r_j, r_x); + icg.endCatchStatement(); + icg.beginFinallyStatement(pos); + icg.beginStatement(pos); + icg.move(r_i, icg.loadImmediate(5)); + icg.endFinallyStatement(); + icg.endTryStatement(); + // switch (i) { case 3: case 4: j = 4; break; case 5: j = 5; break; default : j = 6; } icg.beginSwitchStatement(pos, r_i); @@ -419,8 +453,8 @@ int main(int argc, char **argv) #if 1 assert(JavaScript::Shell::testFactorial(world, 5) == 120); assert(JavaScript::Shell::testObjects(world, 5) == 5); - // JavaScript::Shell::testICG(world); - assert(JavaScript::Shell::testFunctionCall(world, 5) == 5); +// JavaScript::Shell::testICG(world); + assert(JavaScript::Shell::testFunctionCall(world, 5) == 5); #endif JavaScript::Shell::readEvalPrint(stdin, world); return 0;