[pypy-svn] r20911 - in pypy/dist/pypy/translator/js: src test

ericvrp at codespeak.net ericvrp at codespeak.net
Fri Dec 9 00:01:23 CET 2005


Author: ericvrp
Date: Fri Dec  9 00:01:21 2005
New Revision: 20911

Added:
   pypy/dist/pypy/translator/js/src/misc.js
Modified:
   pypy/dist/pypy/translator/js/src/ll_stackless.js
   pypy/dist/pypy/translator/js/src/stack.js
   pypy/dist/pypy/translator/js/test/browsertest.py
   pypy/dist/pypy/translator/js/test/runtest.py
   pypy/dist/pypy/translator/js/test/test_stackless.py
Log:
Some refactoring to getting closer to running long running processes.


Modified: pypy/dist/pypy/translator/js/src/ll_stackless.js
==============================================================================
--- pypy/dist/pypy/translator/js/src/ll_stackless.js	(original)
+++ pypy/dist/pypy/translator/js/src/ll_stackless.js	Fri Dec  9 00:01:21 2005
@@ -3,19 +3,11 @@
 var slp_frame_stack_top    = null;
 var slp_frame_stack_bottom = null;
 var slp_return_value       = undefined;
+
+var slp_timeout            = false;
 var slp_start_time         = undefined;
 var slp_stack_depth        = 0;
 
-// This gets called with --log
-
-function log(s) {
-    try {
-        alert(s);   // in browser
-    } catch (e) {
-        print('log: ' + s);   // commandline
-    }
-}
-
 function function_name(func) {
     var s = func.toString().split("\n");
     s = s[0].length == 0 ? s[1] : s[0];
@@ -24,8 +16,6 @@
     return s
 }
 
-// example function for testing
-
 function ll_stackless_stack_frames_depth() {
     if (!slp_frame_stack_top) {
         LOG("ll_stackless_stack_frames_depth init");
@@ -49,18 +39,17 @@
     var result = slp_stack_depth > 500;   // Firefox has a recursion limit of 1000 (others allow more)
     LOG("ll_stack_to_big result=" + result);
 
-    if (!result) {
+    if (!result && in_browser && false) {
         var t = new Date().getTime();
         var d = t - slp_start_time;
-        result = d > 1000;
+        result = d > 100;
         if (result) {
-            print('XXX d='+d + ' XXX t='+t);
-            slp_start_time = t;
+            slp_timeout = true;
         } 
     }
     return result;
 }
-ll_stack_too_big__ = ll_stack_too_big
+ll_stack_too_big__ = ll_stack_too_big;
 
 function slp_new_frame(targetvar, func, resume_blocknum, vars) {
     //LOG("slp_new_frame("+targetvar+","+function_name(func)+","+resume_blocknum+","+vars.toSource()+")");
@@ -145,6 +134,9 @@
 
 function slp_main_loop() {
     var f_back;
+    log("SLP_MAIN_LOOP");
+    slp_timeout    = false;
+    slp_start_time = new Date().getTime();
     while (true) {
         slp_frame_stack_bottom = null;
         pending = slp_frame_stack_top;
@@ -152,9 +144,12 @@
         while (true) {
             f_back           = pending.f_back;
             LOG('calling: ' + function_name(pending.func));
-            //slp_start_time   = new Date().getTime();    //XXX should really exit javascript and resume with setTimeout(...)
             slp_stack_depth  = 0;               // we are restarting to recurse
             slp_return_value = pending.func();  // params get initialized in the function because it's a resume!
+            if (slp_timeout) {
+                setTimeout('slp_main_loop()', 0);
+                return undefined;
+            }
             if (slp_frame_stack_top) {
                 break;
             }
@@ -164,19 +159,29 @@
             pending             = f_back;
             slp_frame_stack_top = pending;
         }
-        
+
         if (slp_frame_stack_bottom) { // returning from switch()
             if (slp_frame_stack_bottom.f_back) log('slp_frame_stack_bottom.f_back');
             slp_frame_stack_bottom.f_back = f_back;
         }
     }
+    log("REALLY FINISHED");
 }
 
-function slp_entry_point(funcstring) {
+// 
+// note: this function returns undefined for long running processes.
+//        In that case it should be seen as similar to thread.run()
+//
+function slp_entry_point(funcstring) {  //new thread().run()
+    slp_timeout     = false;
     slp_start_time  = new Date().getTime();
     slp_stack_depth = 0;    /// initial stack depth
     var result = eval(funcstring);
     if (slp_frame_stack_bottom) { // get with dispatch loop when stack unwound
+        if (slp_timeout) {
+            setTimeout('slp_main_loop()', 0);
+            return undefined;
+        }
         slp_main_loop();
         result = slp_return_value;
     }

Added: pypy/dist/pypy/translator/js/src/misc.js
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/js/src/misc.js	Fri Dec  9 00:01:21 2005
@@ -0,0 +1,16 @@
+var in_browser;
+try {
+    dummy = alert;
+    in_browser = true;
+} catch (e) {
+    in_browser = false;
+}
+
+function log(s) {
+    if (in_browser) {
+        var logdiv = document.getElementById('logdiv');
+        logdiv.innerHTML = new Date().getTime() + ': ' + s + "<br/>" + logdiv.innerHTML;
+    } else {
+        print('log: ' + s);
+    }
+}

Modified: pypy/dist/pypy/translator/js/src/stack.js
==============================================================================
--- pypy/dist/pypy/translator/js/src/stack.js	(original)
+++ pypy/dist/pypy/translator/js/src/stack.js	Fri Dec  9 00:01:21 2005
@@ -1,7 +1,3 @@
-function logme(s) {
-    print("logme: " + s);
-}
-
 function ll_stack_too_big_helper(depth) {
     if (depth > 0) {
         ll_stack_too_big_helper(depth-1)
@@ -16,3 +12,4 @@
     }
     return false;
 }
+ll_stack_too_big__ = ll_stack_too_big;

Modified: pypy/dist/pypy/translator/js/test/browsertest.py
==============================================================================
--- pypy/dist/pypy/translator/js/test/browsertest.py	(original)
+++ pypy/dist/pypy/translator/js/test/browsertest.py	Fri Dec  9 00:01:21 2005
@@ -29,9 +29,12 @@
             result = "raise Exception('unknown')";
         }
     }
-    var resultform = document.forms['resultform'];
-    resultform.result.value = result;
-    resultform.submit();
+
+    if (result != undefined || !in_browser) {  // if no timeout (i.e. not long running)
+        var resultform = document.forms['resultform'];
+        resultform.result.value = result;
+        resultform.submit();
+    }
 };
 </script>
 </head>
@@ -40,6 +43,7 @@
     <form method="post" name="resultform" id="resultform">
         <input name="result" type="hidden" value="UNKNOWN" />
     </form>
+    <div id="logdiv"></div>
 </body>
 </html>"""
 
@@ -76,6 +80,7 @@
         jstestcase = jstest.jstestcase
         jscode     = jstest.jscode
         html_page  = config.html_page % locals()
+        open("html_page.html", "w").write(html_page)
         self.serve_data('text/html', html_page)
         do_status = 'do_GET'
 

Modified: pypy/dist/pypy/translator/js/test/runtest.py
==============================================================================
--- pypy/dist/pypy/translator/js/test/runtest.py	(original)
+++ pypy/dist/pypy/translator/js/test/runtest.py	Fri Dec  9 00:01:21 2005
@@ -36,7 +36,7 @@
 
         function_call = "%s(%s)" % (self.js.graph.name, args)
         if self.js.stackless:
-            function_call = "slp_entry_point(%s)" % function_call
+            function_call = "slp_entry_point('%s')" % function_call
 
         if use_browsertest:
             output = jstest(self.js.filename, function_call)

Modified: pypy/dist/pypy/translator/js/test/test_stackless.py
==============================================================================
--- pypy/dist/pypy/translator/js/test/test_stackless.py	(original)
+++ pypy/dist/pypy/translator/js/test/test_stackless.py	Fri Dec  9 00:01:21 2005
@@ -181,11 +181,19 @@
     data = wrap_stackless_function(f)
     assert int(data.strip()) == 7495
 
+# XXX
+# need test to detect timeout (return=undefined), call slp_main_loop() until no timeout
+# and only then check result.
+
 def test_long_running():
+    py.test.skip("stackless feature incomplete (no long running processes yet)")
+
     n_iterations = 50000
 
     def g(x):
         if x > 0:
+            for q in range(1000):
+                pass
             g(x-1)
         return x
 
@@ -193,4 +201,6 @@
         return g(n_iterations)
 
     data = wrap_stackless_function(lp)
-    assert int(data.strip()) == n_iterations
+
+    #note: because long running processes can't return a value like this
+    assert int(data.strip()) == undefined



More information about the Pypy-commit mailing list