diff --git a/ravi-tests/ravi_tests1.ravi b/ravi-tests/ravi_tests1.ravi index a9f3c3b..ff7cfec 100644 --- a/ravi-tests/ravi_tests1.ravi +++ b/ravi-tests/ravi_tests1.ravi @@ -121,6 +121,7 @@ opcodes_coverage.TOTYPE = 0 opcodes_coverage.TOSTRING = 0 opcodes_coverage.TOCLOSURE = 0 opcodes_coverage.SELF_SK = 0 +opcodes_coverage.DEFER = 0 local compile = function(f) if ravi.jit() then @@ -1727,6 +1728,49 @@ compile(x) assert(x(2,3) == 2^3) print 'Test 77 OK' +-- Test defer statement +y = 0 +function x() + defer y = y + 1 end + defer y = y + 1 end +end +check(x, 'DEFER', 'CLOSURE', 'DEFER', 'CLOSURE', 'RETURN') +x() +assert(y == 2) +compile(x) +x() +assert(y == 4) +print 'Test 78 OK' + +-- Test defer statement +y = 0 +function x() + defer y = y + 1 end + error('raise error') + defer y = y + 2 end -- will not be called +end +pcall(x) +assert(y == 1) +compile(x) +pcall(x) +assert(y == 2) +print 'Test 79 OK' + +-- Test defer statement +y = 0 +function x() + defer y = y + 1 end + defer y = y + 2; error('err') end + defer y = y + 3 end +end +pcall(x) +assert(y == 6) +compile(x) +pcall(x) +assert(y == 12) +print 'Test 80 OK' + + for k,v in pairs(opcodes_coverage) do print(k, v) diff --git a/ravi-tests/ravi_tests2.ravi b/ravi-tests/ravi_tests2.ravi index dedd4f9..c154f5c 100644 --- a/ravi-tests/ravi_tests2.ravi +++ b/ravi-tests/ravi_tests2.ravi @@ -122,6 +122,7 @@ opcodes_coverage.TABLE_SELF_SK = 0 opcodes_coverage.TOTYPE = 0 opcodes_coverage.TOSTRING = 0 opcodes_coverage.TOCLOSURE = 0 +opcodes_coverage.DEFER = 0 --ravi.verbosity(1) diff --git a/src/ravi_llvmcodegen.cpp b/src/ravi_llvmcodegen.cpp index e70172d..b38e24f 100644 --- a/src/ravi_llvmcodegen.cpp +++ b/src/ravi_llvmcodegen.cpp @@ -1203,7 +1203,7 @@ void RaviCodeGenerator::emit_extern_declarations(RaviFunctionDef *def) { def->types->raviV_op_totypeT, reinterpret_cast(&raviV_op_totype), "raviV_op_totype"); def->raviV_op_deferF = def->raviF->addExternFunction( - def->types->raviV_op_totypeT, reinterpret_cast(&raviV_op_defer), + def->types->raviV_op_deferT, reinterpret_cast(&raviV_op_defer), "raviV_op_defer"); def->ravi_dump_valueF = def->raviF->addExternFunction(