<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[2012] MacRuby/branches/experimental</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.macosforge.org/projects/ruby/changeset/2012">2012</a></dd>
<dt>Author</dt> <dd>lsansonetti@apple.com</dd>
<dt>Date</dt> <dd>2009-07-10 16:42:33 -0700 (Fri, 10 Jul 2009)</dd>
</dl>
<h3>Log Message</h3>
<pre>a better exception-based implementation for return-from-block</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#MacRubybranchesexperimentalcompilercpp">MacRuby/branches/experimental/compiler.cpp</a></li>
<li><a href="#MacRubybranchesexperimentalcompilerh">MacRuby/branches/experimental/compiler.h</a></li>
<li><a href="#MacRubybranchesexperimentaltest_vmblockrb">MacRuby/branches/experimental/test_vm/block.rb</a></li>
<li><a href="#MacRubybranchesexperimentalvmcpp">MacRuby/branches/experimental/vm.cpp</a></li>
<li><a href="#MacRubybranchesexperimentalvmh">MacRuby/branches/experimental/vm.h</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="MacRubybranchesexperimentalcompilercpp"></a>
<div class="modfile"><h4>Modified: MacRuby/branches/experimental/compiler.cpp (2011 => 2012)</h4>
<pre class="diff"><span>
<span class="info">--- MacRuby/branches/experimental/compiler.cpp        2009-07-10 01:56:08 UTC (rev 2011)
+++ MacRuby/branches/experimental/compiler.cpp        2009-07-10 23:42:33 UTC (rev 2012)
</span><span class="lines">@@ -32,9 +32,9 @@
</span><span class="cx"> llvm::Module *RoxorCompiler::module = NULL;
</span><span class="cx"> RoxorCompiler *RoxorCompiler::shared = NULL;
</span><span class="cx">
</span><del>-RoxorCompiler::RoxorCompiler(const char *_fname)
</del><ins>+RoxorCompiler::RoxorCompiler(void)
</ins><span class="cx"> {
</span><del>- fname = _fname;
</del><ins>+ fname = NULL;
</ins><span class="cx"> inside_eval = false;
</span><span class="cx">
</span><span class="cx"> bb = NULL;
</span><span class="lines">@@ -58,7 +58,8 @@
</span><span class="cx"> current_loop_end_bb = NULL;
</span><span class="cx"> current_loop_exit_val = NULL;
</span><span class="cx"> current_rescue = false;
</span><del>- return_from_block = false;
</del><ins>+ return_from_block = -1;
+ return_from_block_ids = 0;
</ins><span class="cx">
</span><span class="cx"> dispatcherFunc = NULL;
</span><span class="cx"> fastEqqFunc = NULL;
</span><span class="lines">@@ -114,7 +115,7 @@
</span><span class="cx"> getSpecialFunc = NULL;
</span><span class="cx"> breakFunc = NULL;
</span><span class="cx"> returnFromBlockFunc = NULL;
</span><del>- returnFromBlockValueFunc = NULL;
</del><ins>+ checkReturnFromBlockFunc = NULL;
</ins><span class="cx"> longjmpFunc = NULL;
</span><span class="cx"> setjmpFunc = NULL;
</span><span class="cx"> popBrokenValue = NULL;
</span><span class="lines">@@ -147,8 +148,8 @@
</span><span class="cx"> #endif
</span><span class="cx"> }
</span><span class="cx">
</span><del>-RoxorAOTCompiler::RoxorAOTCompiler(const char *_fname)
- : RoxorCompiler(_fname)
</del><ins>+RoxorAOTCompiler::RoxorAOTCompiler(void)
+: RoxorCompiler()
</ins><span class="cx"> {
</span><span class="cx"> cObject_gvar = NULL;
</span><span class="cx"> name2symFunc = NULL;
</span><span class="lines">@@ -1445,32 +1446,40 @@
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> void
</span><del>-RoxorCompiler::compile_return_from_block(Value *val)
</del><ins>+RoxorCompiler::compile_return_from_block(Value *val, int id)
</ins><span class="cx"> {
</span><span class="cx"> if (returnFromBlockFunc == NULL) {
</span><del>-        // void rb_vm_return_from_block(VALUE val);
</del><ins>+        // void rb_vm_return_from_block(VALUE val, int id);
</ins><span class="cx">         returnFromBlockFunc = cast<Function>(
</span><span class="cx">                 module->getOrInsertFunction("rb_vm_return_from_block",
</span><del>-                 Type::VoidTy, RubyObjTy, NULL));
</del><ins>+                 Type::VoidTy, RubyObjTy, Type::Int32Ty, NULL));
</ins><span class="cx"> }
</span><span class="cx"> std::vector<Value *> params;
</span><span class="cx"> params.push_back(val);
</span><ins>+ params.push_back(ConstantInt::get(Type::Int32Ty, id));
</ins><span class="cx"> CallInst::Create(returnFromBlockFunc, params.begin(), params.end(), "", bb);
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> void
</span><del>-RoxorCompiler::compile_return_from_block_handler(void)
</del><ins>+RoxorCompiler::compile_return_from_block_handler(int id)
</ins><span class="cx"> {
</span><del>- compile_landing_pad_header();
</del><ins>+ //const std::type_info &eh_type = typeid(RoxorReturnFromBlockException *);
+ //Value *exception = compile_landing_pad_header(eh_type);
+ Value *exception = compile_landing_pad_header();
</ins><span class="cx">
</span><del>- if (returnFromBlockValueFunc == NULL) {
-        returnFromBlockValueFunc = cast<Function>(
</del><ins>+ if (checkReturnFromBlockFunc == NULL) {
+        // VALUE rb_vm_check_return_from_block_exc(void *exc, int id);
+        checkReturnFromBlockFunc = cast<Function>(
</ins><span class="cx">                 module->getOrInsertFunction(
</span><del>-                 "rb_vm_pop_return_from_block_value",
-                 RubyObjTy, NULL));
</del><ins>+                 "rb_vm_check_return_from_block_exc",
+                 RubyObjTy, PtrTy, Type::Int32Ty, NULL));
</ins><span class="cx"> }
</span><span class="cx">
</span><del>- Value *val = CallInst::Create(returnFromBlockValueFunc, "", bb);
</del><ins>+ std::vector<Value *> params;
+ params.push_back(exception);
+ params.push_back(ConstantInt::get(Type::Int32Ty, id));
+ Value *val = CallInst::Create(checkReturnFromBlockFunc, params.begin(),
+         params.end(), "", bb);
</ins><span class="cx">
</span><span class="cx"> Function *f = bb->getParent();
</span><span class="cx"> BasicBlock *ret_bb = BasicBlock::Create("ret", f);
</span><span class="lines">@@ -1558,8 +1567,10 @@
</span><span class="cx">                 compile_pop_exception();
</span><span class="cx">          }
</span><span class="cx">          if (within_block) {
</span><del>-                return_from_block = true;
-                compile_return_from_block(val);
</del><ins>+                if (return_from_block == -1) {
+                 return_from_block = return_from_block_ids++;
+                }
+                compile_return_from_block(val, return_from_block);
</ins><span class="cx">                 ReturnInst::Create(val, bb);
</span><span class="cx">          }
</span><span class="cx">          else {
</span><span class="lines">@@ -1596,9 +1607,15 @@
</span><span class="cx"> return compile_current_class();
</span><span class="cx"> }
</span><span class="cx">
</span><del>-void
</del><ins>+Value *
</ins><span class="cx"> RoxorCompiler::compile_landing_pad_header(void)
</span><span class="cx"> {
</span><ins>+ return compile_landing_pad_header(typeid(void));
+}
+
+Value *
+RoxorCompiler::compile_landing_pad_header(const std::type_info &eh_type)
+{
</ins><span class="cx"> Function *eh_exception_f = Intrinsic::getDeclaration(module,
</span><span class="cx">          Intrinsic::eh_exception);
</span><span class="cx"> Value *eh_ptr = CallInst::Create(eh_exception_f, "", bb);
</span><span class="lines">@@ -1621,21 +1638,58 @@
</span><span class="cx"> }
</span><span class="cx"> params.push_back(new BitCastInst(__gxx_personality_v0_func,
</span><span class="cx">                 PtrTy, "", bb));
</span><del>- params.push_back(compile_const_pointer(NULL));
</del><span class="cx">
</span><del>- CallInst::Create(eh_selector_f, params.begin(), params.end(),
-         "", bb);
</del><ins>+ if (eh_type == typeid(void)) {
+        // catch (...)
+        params.push_back(compile_const_pointer(NULL));
+ }
+ else {
+        // catch (eh_type &exc)
+        params.push_back(compile_const_pointer((void *)&eh_type));
+        params.push_back(compile_const_pointer(NULL));
+ }
</ins><span class="cx">
</span><ins>+ Value *eh_sel = CallInst::Create(eh_selector_f, params.begin(),
+         params.end(), "", bb);
+
+ if (eh_type != typeid(void)) {
+        // TODO: this doesn't work yet, the type id must be a GlobalVariable...
+#if __LP64__
+        Function *eh_typeid_for_f = Intrinsic::getDeclaration(module,
+                Intrinsic::eh_typeid_for_i64);
+#else
+        Function *eh_typeid_for_f = Intrinsic::getDeclaration(module,
+                Intrinsic::eh_typeid_for_i32);
+#endif
+        std::vector<Value *> params;
+        params.push_back(compile_const_pointer((void *)&eh_type));
+
+        Value *eh_typeid = CallInst::Create(eh_typeid_for_f, params.begin(),
+                params.end(), "", bb);
+
+        Function *f = bb->getParent();
+        BasicBlock *typeok_bb = BasicBlock::Create("typeok", f);
+        BasicBlock *nocatch_bb = BasicBlock::Create("nocatch", f);
+        Value *need_ret = new ICmpInst(ICmpInst::ICMP_EQ, eh_sel,
+                eh_typeid, "", bb);
+        BranchInst::Create(typeok_bb, nocatch_bb, need_ret, bb);
+
+        bb = nocatch_bb;
+        compile_rethrow_exception();
+
+        bb = typeok_bb;
+ }
+
</ins><span class="cx"> Function *beginCatchFunc = NULL;
</span><span class="cx"> if (beginCatchFunc == NULL) {
</span><span class="cx">         // void *__cxa_begin_catch(void *);
</span><span class="cx">         beginCatchFunc = cast<Function>(
</span><span class="cx">                 module->getOrInsertFunction("__cxa_begin_catch",
</span><del>-                 Type::VoidTy, PtrTy, NULL));
</del><ins>+                 PtrTy, PtrTy, NULL));
</ins><span class="cx"> }
</span><span class="cx"> params.clear();
</span><span class="cx"> params.push_back(eh_ptr);
</span><del>- CallInst::Create(beginCatchFunc, params.begin(), params.end(),
</del><ins>+ return CallInst::Create(beginCatchFunc, params.begin(), params.end(),
</ins><span class="cx">          "", bb);
</span><span class="cx"> }
</span><span class="cx">
</span><span class="lines">@@ -4245,7 +4299,6 @@
</span><span class="cx">                  compile_landing_pad_header();
</span><span class="cx">                  compile_node(node->nd_ensr);
</span><span class="cx">                  compile_rethrow_exception();
</span><del>-                 //compile_landing_pad_footer();
</del><span class="cx">                 }
</span><span class="cx">                 else {
</span><span class="cx">                  val = compile_node(node->nd_head);
</span><span class="lines">@@ -4331,7 +4384,7 @@
</span><span class="cx">                 NODE *old_current_block_node = current_block_node;
</span><span class="cx">                 ID old_current_mid = current_mid;
</span><span class="cx">                 bool old_current_block = current_block;
</span><del>-                bool old_return_from_block = return_from_block;
</del><ins>+                int old_return_from_block = return_from_block;
</ins><span class="cx">                 BasicBlock *old_rescue_bb = rescue_bb;
</span><span class="cx">
</span><span class="cx">                 current_mid = 0;
</span><span class="lines">@@ -4342,7 +4395,7 @@
</span><span class="cx">                 assert(Function::classof(block));
</span><span class="cx">
</span><span class="cx">                 BasicBlock *return_from_block_bb = NULL;
</span><del>-                if (!old_return_from_block && return_from_block) {
</del><ins>+                if (return_from_block != -1) {
</ins><span class="cx">                  // The block we just compiled contains one or more
</span><span class="cx">                  // return expressions! We need to enclose the dispatcher
</span><span class="cx">                  // call inside an exception handler, since return-from
</span><span class="lines">@@ -4381,10 +4434,10 @@
</span><span class="cx">                  caller = compile_dispatch_call(params);
</span><span class="cx">                 }
</span><span class="cx">
</span><del>-                if (return_from_block_bb) {
</del><ins>+                if (return_from_block != -1) {
</ins><span class="cx">                  BasicBlock *old_bb = bb;
</span><span class="cx">                  bb = return_from_block_bb;
</span><del>-                 compile_return_from_block_handler();        
</del><ins>+                 compile_return_from_block_handler(return_from_block);        
</ins><span class="cx">                  rescue_bb = old_rescue_bb;
</span><span class="cx">                  bb = old_bb;
</span><span class="cx">                 }
</span><span class="lines">@@ -4415,10 +4468,10 @@
</span><span class="cx">                 if (node->nd_head != NULL) {
</span><span class="cx">                  compile_dispatch_arguments(node->nd_head, params, &argc);
</span><span class="cx">                 }
</span><del>-                params.insert(params.begin(), ConstantInt::get(Type::Int32Ty, argc));
</del><ins>+                params.insert(params.begin(),
+                        ConstantInt::get(Type::Int32Ty, argc));
</ins><span class="cx">
</span><del>-                return CallInst::Create(yieldFunc, params.begin(),
-                        params.end(), "", bb);
</del><ins>+                return compile_protected_call(yieldFunc, params);
</ins><span class="cx">          }
</span><span class="cx">          break;
</span><span class="cx">
</span></span></pre></div>
<a id="MacRubybranchesexperimentalcompilerh"></a>
<div class="modfile"><h4>Modified: MacRuby/branches/experimental/compiler.h (2011 => 2012)</h4>
<pre class="diff"><span>
<span class="info">--- MacRuby/branches/experimental/compiler.h        2009-07-10 01:56:08 UTC (rev 2011)
+++ MacRuby/branches/experimental/compiler.h        2009-07-10 23:42:33 UTC (rev 2012)
</span><span class="lines">@@ -30,9 +30,13 @@
</span><span class="cx">         static llvm::Module *module;
</span><span class="cx">         static RoxorCompiler *shared;
</span><span class="cx">
</span><del>-        RoxorCompiler(const char *fname);
</del><ins>+        RoxorCompiler(void);
</ins><span class="cx">         virtual ~RoxorCompiler(void) { }
</span><span class="cx">
</span><ins>+        void set_fname(const char *_fname) {
+         fname = _fname;
+        }
+
</ins><span class="cx">         Value *compile_node(NODE *node);
</span><span class="cx">
</span><span class="cx">         virtual Function *compile_main_function(NODE *node);
</span><span class="lines">@@ -92,7 +96,8 @@
</span><span class="cx">         BasicBlock *current_loop_body_bb;
</span><span class="cx">         BasicBlock *current_loop_end_bb;
</span><span class="cx">         Value *current_loop_exit_val;
</span><del>-        bool return_from_block;
</del><ins>+        int return_from_block;
+        int return_from_block_ids;
</ins><span class="cx">
</span><span class="cx">         Function *dispatcherFunc;
</span><span class="cx">         Function *fastEqqFunc;
</span><span class="lines">@@ -148,7 +153,7 @@
</span><span class="cx">         Function *getSpecialFunc;
</span><span class="cx">         Function *breakFunc;
</span><span class="cx">         Function *returnFromBlockFunc;
</span><del>-        Function *returnFromBlockValueFunc;
</del><ins>+        Function *checkReturnFromBlockFunc;
</ins><span class="cx">         Function *longjmpFunc;
</span><span class="cx">         Function *setjmpFunc;
</span><span class="cx">         Function *popBrokenValue;
</span><span class="lines">@@ -229,8 +234,8 @@
</span><span class="cx">         Value *compile_dstr(NODE *node);
</span><span class="cx">         Value *compile_dvar_slot(ID name);
</span><span class="cx">         void compile_break_val(Value *val);
</span><del>-        void compile_return_from_block(Value *val);
-        void compile_return_from_block_handler(void);
</del><ins>+        void compile_return_from_block(Value *val, int id);
+        void compile_return_from_block_handler(int id);
</ins><span class="cx">         Value *compile_jump(NODE *node);
</span><span class="cx">         virtual Value *compile_mcache(SEL sel, bool super);
</span><span class="cx">         virtual Value *compile_ccache(ID id);
</span><span class="lines">@@ -248,7 +253,8 @@
</span><span class="cx">         virtual Value *compile_immutable_literal(VALUE val);
</span><span class="cx">         virtual Value *compile_global_entry(NODE *node);
</span><span class="cx">
</span><del>-        void compile_landing_pad_header(void);
</del><ins>+        Value *compile_landing_pad_header(void);
+        Value *compile_landing_pad_header(const std::type_info &eh_type);
</ins><span class="cx">         void compile_landing_pad_footer(bool pop_exception=true);
</span><span class="cx">         void compile_rethrow_exception(void);
</span><span class="cx">         void compile_pop_exception(void);
</span><span class="lines">@@ -286,7 +292,7 @@
</span><span class="cx">
</span><span class="cx"> class RoxorAOTCompiler : public RoxorCompiler {
</span><span class="cx"> public:
</span><del>-        RoxorAOTCompiler(const char *fname);
</del><ins>+        RoxorAOTCompiler(void);
</ins><span class="cx">
</span><span class="cx">         Function *compile_main_function(NODE *node);
</span><span class="cx">
</span></span></pre></div>
<a id="MacRubybranchesexperimentaltest_vmblockrb"></a>
<div class="modfile"><h4>Modified: MacRuby/branches/experimental/test_vm/block.rb (2011 => 2012)</h4>
<pre class="diff"><span>
<span class="info">--- MacRuby/branches/experimental/test_vm/block.rb        2009-07-10 01:56:08 UTC (rev 2011)
+++ MacRuby/branches/experimental/test_vm/block.rb        2009-07-10 23:42:33 UTC (rev 2012)
</span><span class="lines">@@ -527,6 +527,39 @@
</span><span class="cx"> p bar
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+assert ':ok', %{
+ def foo
+ begin
+ yield
+ ensure
+ p :ok
+ end
+ end
+ def bar
+ foo { return }
+ end
+ bar
+}
+
+assert 'false', %{
+ def foo(m); m.synchronize { return 42 }; end
+ m = Mutex.new
+ foo(m)
+ p m.locked?
+}
+
+assert ':ok', %{
+ def foo(v)
+ 1.times do
+ return true if v
+ return false
+ p :nok1
+ end
+ p :nok2
+ end
+ p :ok if !foo(false) and foo(true)
+}
+
</ins><span class="cx"> assert ":ok\n:ok", %{
</span><span class="cx"> def foo
</span><span class="cx"> raise
</span></span></pre></div>
<a id="MacRubybranchesexperimentalvmcpp"></a>
<div class="modfile"><h4>Modified: MacRuby/branches/experimental/vm.cpp (2011 => 2012)</h4>
<pre class="diff"><span>
<span class="info">--- MacRuby/branches/experimental/vm.cpp        2009-07-10 01:56:08 UTC (rev 2011)
+++ MacRuby/branches/experimental/vm.cpp        2009-07-10 23:42:33 UTC (rev 2012)
</span><span class="lines">@@ -180,7 +180,7 @@
</span><span class="cx"> #define VALUE_TO_GV(v) (value2gv((VALUE)v))
</span><span class="cx">
</span><span class="cx"> extern "C" void *__cxa_allocate_exception(size_t);
</span><del>-extern "C" void __cxa_throw(void *, void *, void *);
</del><ins>+extern "C" void __cxa_throw(void *, void *, void (*)(void*));
</ins><span class="cx">
</span><span class="cx"> RoxorCore::RoxorCore(void)
</span><span class="cx"> {
</span><span class="lines">@@ -237,7 +237,6 @@
</span><span class="cx"> safe_level = 0;
</span><span class="cx"> backref = Qnil;
</span><span class="cx"> broken_with = Qundef;
</span><del>- returned_from_block = false;
</del><span class="cx"> last_status = Qnil;
</span><span class="cx"> errinfo = Qnil;
</span><span class="cx"> parse_in_eval = false;
</span><span class="lines">@@ -293,7 +292,6 @@
</span><span class="cx">
</span><span class="cx"> backref = Qnil;
</span><span class="cx"> broken_with = Qundef;
</span><del>- returned_from_block = false;
</del><span class="cx"> last_status = Qnil;
</span><span class="cx"> errinfo = Qnil;
</span><span class="cx"> parse_in_eval = false;
</span><span class="lines">@@ -3835,6 +3833,45 @@
</span><span class="cx"> return val;
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+extern "C"
+void
+rb_vm_return_from_block(VALUE val, int id)
+{
+ RoxorReturnFromBlockException *exc = new RoxorReturnFromBlockException();
+
+ rb_objc_retain((void *)val);
+ exc->val = val;
+ exc->id = id;
+
+ throw exc;
+}
+
+extern "C" std::type_info *__cxa_current_exception_type(void);
+
+static inline bool
+current_exception_is_return_from_block(void)
+{
+ const std::type_info *exc_type = __cxa_current_exception_type();
+ return exc_type != NULL
+        && *exc_type == typeid(RoxorReturnFromBlockException *);
+}
+
+extern "C"
+VALUE
+rb_vm_check_return_from_block_exc(RoxorReturnFromBlockException **pexc, int id)
+{
+ if (current_exception_is_return_from_block()) {
+        RoxorReturnFromBlockException *exc = *pexc;
+        if (id == -1 || exc->id == id) {
+         VALUE val = exc->val;
+         rb_objc_release((void *)val);
+         delete exc;
+         return val;
+        }
+ }
+ return Qundef;
+}
+
</ins><span class="cx"> static inline void
</span><span class="cx"> rb_vm_rethrow(void)
</span><span class="cx"> {
</span><span class="lines">@@ -3842,27 +3879,19 @@
</span><span class="cx"> __cxa_throw(exc, NULL, NULL);
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+#if 0
</ins><span class="cx"> extern "C"
</span><del>-void
-rb_vm_return_from_block(VALUE val)
-{
- GET_VM()->set_broken_with(val);
- GET_VM()->set_returned_from_block(true);
- rb_vm_rethrow();
-}
-
-extern "C"
</del><span class="cx"> VALUE
</span><del>-rb_vm_pop_return_from_block_value(void)
</del><ins>+rb_vm_pop_return_from_block_value(int id)
</ins><span class="cx"> {
</span><del>- if (GET_VM()->get_returned_from_block()) {
-        GET_VM()->set_returned_from_block(false);
</del><ins>+ if (GET_VM()->check_return_from_block(id)) {
</ins><span class="cx">         VALUE val = rb_vm_pop_broken_value();
</span><span class="cx">         assert(val != Qundef);
</span><span class="cx">         return val;
</span><span class="cx"> }
</span><span class="cx"> return Qundef;
</span><span class="cx"> }
</span><ins>+#endif
</ins><span class="cx">
</span><span class="cx"> extern "C"
</span><span class="cx"> VALUE
</span><span class="lines">@@ -4023,8 +4052,8 @@
</span><span class="cx"> rb_vm_init_compiler(void)
</span><span class="cx"> {
</span><span class="cx"> RoxorCompiler::shared = ruby_aot_compile
</span><del>-        ? new RoxorAOTCompiler("")
-        : new RoxorCompiler("");
</del><ins>+        ? new RoxorAOTCompiler()
+        : new RoxorCompiler();
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> extern "C"
</span><span class="lines">@@ -4041,8 +4070,10 @@
</span><span class="cx"> RoxorCompiler *compiler = RoxorCompiler::shared;
</span><span class="cx">
</span><span class="cx"> bool old_inside_eval = compiler->is_inside_eval();
</span><del>- compiler->set_inside_eval(inside_eval);
</del><ins>+ compiler->set_inside_eval(inside_eval);
+ compiler->set_fname(fname);
</ins><span class="cx"> Function *function = compiler->compile_main_function(node);
</span><ins>+ compiler->set_fname(NULL);
</ins><span class="cx"> compiler->set_inside_eval(old_inside_eval);
</span><span class="cx">
</span><span class="cx"> if (binding != NULL) {
</span><span class="lines">@@ -4514,7 +4545,8 @@
</span><span class="cx"> }
</span><span class="cx"> catch (...) {
</span><span class="cx">         VALUE exc;
</span><del>-        if (rb_vm_pop_return_from_block_value() != Qundef) {
</del><ins>+        if (current_exception_is_return_from_block()) {
+         // TODO: the exception is leaking!
</ins><span class="cx">          exc = rb_exc_new2(rb_eLocalJumpError,
</span><span class="cx">                  "unexpected return from Thread");
</span><span class="cx">         }
</span></span></pre></div>
<a id="MacRubybranchesexperimentalvmh"></a>
<div class="modfile"><h4>Modified: MacRuby/branches/experimental/vm.h (2011 => 2012)</h4>
<pre class="diff"><span>
<span class="info">--- MacRuby/branches/experimental/vm.h        2009-07-10 01:56:08 UTC (rev 2011)
+++ MacRuby/branches/experimental/vm.h        2009-07-10 23:42:33 UTC (rev 2012)
</span><span class="lines">@@ -750,7 +750,6 @@
</span><span class="cx">         VALUE current_top_object;
</span><span class="cx">         VALUE backref;
</span><span class="cx">         VALUE broken_with;
</span><del>-        bool returned_from_block;
</del><span class="cx">         VALUE last_status;
</span><span class="cx">         VALUE errinfo;
</span><span class="cx">         int safe_level;
</span><span class="lines">@@ -767,7 +766,6 @@
</span><span class="cx">         ACCESSOR(current_top_object, VALUE);
</span><span class="cx">         ACCESSOR(backref, VALUE);
</span><span class="cx">         ACCESSOR(broken_with, VALUE);
</span><del>-        ACCESSOR(returned_from_block, bool);
</del><span class="cx">         ACCESSOR(last_status, VALUE);
</span><span class="cx">         ACCESSOR(errinfo, VALUE);
</span><span class="cx">         ACCESSOR(safe_level, int);
</span><span class="lines">@@ -877,6 +875,13 @@
</span><span class="cx"> #define GET_VM() (RoxorVM::current())
</span><span class="cx"> #define GET_THREAD() (GetThreadPtr(GET_VM()->get_thread()))
</span><span class="cx">
</span><ins>+// Custom C++ exception class used to implement "return-from-block".
+class RoxorReturnFromBlockException {
+ public:
+        VALUE val;
+        int id;
+};
+
</ins><span class="cx"> #endif /* __cplusplus */
</span><span class="cx">
</span><span class="cx"> #endif /* __VM_H_ */
</span></span></pre>
</div>
</div>
</body>
</html>