Skip to content
Browse files

topStackFrame is now Object not "global"

git-svn-id: http://hotruby.googlecode.com/svn/trunk@32 4fb8041d-b042-0410-9571-f50be27af959
  • Loading branch information...
1 parent 7d13a51 commit 6c3d3a3d6c542ef12dfb28dcdaee3c4208f0aa5a yukoba@accelart.jp committed Jan 27, 2008
Showing with 51 additions and 45 deletions.
  1. +1 −7 src/RubyNative.js
  2. +7 −14 src/RubyVM.js
  3. +8 −21 web/js/HotRuby.js
  4. +35 −3 web/test-local/insertionSort.html
View
8 src/RubyNative.js
@@ -1,8 +1,5 @@
// The license of this source is "Ruby License"
HotRuby.prototype.classes = {
- "<global>" : {
- },
-
"Object" : {
"==" : function(recver, args) {
return recver == args[0] ? this.trueObj : this.falseObj;
@@ -414,12 +411,9 @@ HotRuby.prototype.classes = {
if(!("__classVars" in classes[className]))
classes[className].__classVars = {};
}
-
- classes.Object.__parentClass = classes["<global>"];
- classes["<global>"].__parentClass = null;
+ classes.Object.__parentClass = null;
for (var className in classes) {
classes.Object.__constantVars[className] = classes[className];
}
- delete classes.Object.__constantVars["<global>"];
})();
View
21 src/RubyVM.js
@@ -44,6 +44,7 @@ var HotRuby = function() {
__className : "Object",
__native : {}
};
+ this.topSF = null;
this.checkEnv();
};
@@ -118,7 +119,7 @@ HotRuby.prototype = {
*/
run : function(opcode) {
try {
- this.runOpcode(opcode, this.classes["<global>"], null, this.topObject, [], null, false, null);
+ this.runOpcode(opcode, this.classes.Object, null, this.topObject, [], null, false, null);
} catch(e) {
alert(e);
}
@@ -152,6 +153,8 @@ HotRuby.prototype = {
sf.isProc = isProc;
sf.cbaseObj = cbaseObj;
+ if(this.topSF == null) this.topSF = sf;
+
// Copy args to localVars. Fill from last.
for (var i = 0;i < opcode[4].arg_size; i++) {
sf.localVars[sf.localVars.length - 1 - i] = args[i];
@@ -840,9 +843,6 @@ HotRuby.prototype = {
// TODO
throw "[setConstant] Not implemented";
}
- // Const in <global> belongs to Object
- if(classObj.__className == "<global>")
- classObj = this.classes.Object;
classObj.__constantVars[constName] = constValue;
},
@@ -859,7 +859,7 @@ HotRuby.prototype = {
var isFound = false;
// Search outer(parentStackFrame)
for (var checkSF = sf;!isFound; checkSF = checkSF.parentStackFrame) {
- if (checkSF.classObj.__className == "<global>") {
+ if (checkSF == this.topSF) {
break;
}
if (constName in checkSF.classObj.__constantVars) {
@@ -869,7 +869,7 @@ HotRuby.prototype = {
}
// Search parent class
if (!isFound) {
- for (classObj = sf.classObj;classObj.__className != "<global>";) {
+ for (classObj = sf.classObj;classObj != this.classes.Object;) {
if (constName in classObj.__constantVars) {
isFound = true;
break;
@@ -887,9 +887,6 @@ HotRuby.prototype = {
}
if (classObj == null || classObj == this.nilObj)
throw "[getConstant] Cannot find constant : " + constName;
- // Const in <global> belongs to Object
- if (classObj.__className == "<global>")
- classObj = this.classes.Object;
return classObj.__constantVars[constName];
},
@@ -900,16 +897,12 @@ HotRuby.prototype = {
*/
getClassName : function(obj) {
if (obj == null)
- return "<global>";
+ return "Object";
switch (typeof(obj)) {
case "object" :
return obj.__className;
case "number" :
return "Float";
-// case "string" :
-// return "String";
-// case "boolean" :
-// return obj ? "TrueClass" : "FalseClass";
default :
throw "[getClassName] unknown type : " + typeof(obj);
}
View
29 web/js/HotRuby.js
@@ -44,6 +44,7 @@ var HotRuby = function() {
__className : "Object",
__native : {}
};
+ this.topSF = null;
this.checkEnv();
};
@@ -118,7 +119,7 @@ HotRuby.prototype = {
*/
run : function(opcode) {
try {
- this.runOpcode(opcode, this.classes["<global>"], null, this.topObject, [], null, false, null);
+ this.runOpcode(opcode, this.classes.Object, null, this.topObject, [], null, false, null);
} catch(e) {
alert(e);
}
@@ -152,6 +153,8 @@ HotRuby.prototype = {
sf.isProc = isProc;
sf.cbaseObj = cbaseObj;
+ if(this.topSF == null) this.topSF = sf;
+
// Copy args to localVars. Fill from last.
for (var i = 0;i < opcode[4].arg_size; i++) {
sf.localVars[sf.localVars.length - 1 - i] = args[i];
@@ -840,9 +843,6 @@ HotRuby.prototype = {
// TODO
throw "[setConstant] Not implemented";
}
- // Const in <global> belongs to Object
- if(classObj.__className == "<global>")
- classObj = this.classes.Object;
classObj.__constantVars[constName] = constValue;
},
@@ -859,7 +859,7 @@ HotRuby.prototype = {
var isFound = false;
// Search outer(parentStackFrame)
for (var checkSF = sf;!isFound; checkSF = checkSF.parentStackFrame) {
- if (checkSF.classObj.__className == "<global>") {
+ if (checkSF == this.topSF) {
break;
}
if (constName in checkSF.classObj.__constantVars) {
@@ -869,7 +869,7 @@ HotRuby.prototype = {
}
// Search parent class
if (!isFound) {
- for (classObj = sf.classObj;classObj.__className != "<global>";) {
+ for (classObj = sf.classObj;classObj != this.classes.Object;) {
if (constName in classObj.__constantVars) {
isFound = true;
break;
@@ -887,9 +887,6 @@ HotRuby.prototype = {
}
if (classObj == null || classObj == this.nilObj)
throw "[getConstant] Cannot find constant : " + constName;
- // Const in <global> belongs to Object
- if (classObj.__className == "<global>")
- classObj = this.classes.Object;
return classObj.__constantVars[constName];
},
@@ -900,16 +897,12 @@ HotRuby.prototype = {
*/
getClassName : function(obj) {
if (obj == null)
- return "<global>";
+ return "Object";
switch (typeof(obj)) {
case "object" :
return obj.__className;
case "number" :
return "Float";
-// case "string" :
-// return "String";
-// case "boolean" :
-// return obj ? "TrueClass" : "FalseClass";
default :
throw "[getClassName] unknown type : " + typeof(obj);
}
@@ -1117,9 +1110,6 @@ HotRuby.VM_CALL_VCALL_BIT = 16;
// The license of this source is "Ruby License"
HotRuby.prototype.classes = {
- "<global>" : {
- },
-
"Object" : {
"==" : function(recver, args) {
return recver == args[0] ? this.trueObj : this.falseObj;
@@ -1531,12 +1521,9 @@ HotRuby.prototype.classes = {
if(!("__classVars" in classes[className]))
classes[className].__classVars = {};
}
-
- classes.Object.__parentClass = classes["<global>"];
- classes["<global>"].__parentClass = null;
+ classes.Object.__parentClass = null;
for (var className in classes) {
classes.Object.__constantVars[className] = classes[className];
}
- delete classes.Object.__constantVars["<global>"];
})();
View
38 web/test-local/insertionSort.html
@@ -1,11 +1,43 @@
<html>
<head>
<title>Insertion sort - HotRuby - Ruby on JavaScript</title>
+ <link href="../css/prettify.css" type="text/css" rel="stylesheet" />
+ <script type="text/javascript" src="../js/prettify.js"></script>
<script type="text/javascript" src="../js/HotRuby.js"></script>
<script type="text/javascript" src="../js/test/InsertionSort.js"></script>
</head>
-<body onload="new HotRuby().run(src);">
- <h1>Insertion Sort - HotRuby</h1>
- <div id="debug"></div>
+<body onload="prettyPrint(); new HotRuby().run(src);">
+ <h1>Insertion Sort - HotRuby(Ruby on JavaScript)</h1>
+ <div>Result:</div>
+ <div id="debug" class="result" style="color:black"></div>
+ <br />
+ <div>Source:<pre class="prettyprint">
+class InsertionSort
+ msg = "[ Insertion Sort ]"
+ puts msg
+
+ def sort ary
+ puts "Before insertion sort:"
+ puts ary
+
+ for i in 1..(ary.length-1) do
+ n = i
+ while n >= 1 && ary[n] < ary[n - 1] do
+ if ary[n] < ary[n - 1]
+ tmp = ary[n]
+ ary[n] = ary[n - 1]
+ ary[n - 1] = tmp
+ end
+ n -= 1
+ end
+ end
+
+ puts "After insertion sort:"
+ puts ary
+ end
+end
+
+InsertionSort.new.sort [3, 6, 2, 5, 3, 7, 1, 8]
+</pre></div>
</body>
</html>

0 comments on commit 6c3d3a3

Please sign in to comment.
Something went wrong with that request. Please try again.