Permalink
Browse files

Support for Flash

git-svn-id: http://hotruby.googlecode.com/svn/trunk@11 4fb8041d-b042-0410-9571-f50be27af959
  • Loading branch information...
1 parent b188c88 commit db51557285fe9bcfa330412453ae3d247869926d yukoba@accelart.jp committed Jan 14, 2008
View

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -0,0 +1,16 @@
+package {
+ import flash.display.*;
+ public class HotRubyFlash extends Sprite {
+ public function HotRubyFlash() {
+ _root = this;
+ new HotRuby().run(src);
+ }
+ }
+}
+
+import flash.display.*;
+import flash.text.*;
+
+var _root = null;
+
+
@@ -0,0 +1,6 @@
+HowToDebug 2008/01/14 17:54
+
+Build with buildHotRubyDebug.bat
+Run C:\Program Files\flex2_sdk_hf1\bin\fdb.exe
+Enter "run file:///C:/Documents/my/2007/HotRuby/ActionScript/HotRuby.swf"
+Enter "continue"
@@ -0,0 +1,5 @@
+@echo off
+set "PATH=C:\Program Files\flex2_sdk_hf1\bin;%PATH%"
+copy /b HotRubyHeader.as + test.as + ..\js\HotRuby.js + ..\js\NativeMethods.js HotRubyFlash.as
+mxmlc --strict=false --warnings=false -default-size 600 400 -default-frame-rate=30 -default-background-color=0xFFFFFF HotRubyFlash.as
+pause
@@ -0,0 +1,5 @@
+@echo off
+set "PATH=C:\Program Files\flex2_sdk_hf1\bin;%PATH%"
+copy /b HotRubyHeader.as + test.as + ..\js\HotRuby.js + ..\js\NativeMethods.js HotRubyFlash.as
+mxmlc -debug=true --strict=false --warnings=false -default-size 600 400 -default-frame-rate=30 -default-background-color=0xFFFFFF HotRubyFlash.as
+pause
View
@@ -0,0 +1 @@
+var src = ["YARVInstructionSequence\/SimpleDataFormat",1,1,1,{"arg_size":0,"local_size":1,"stack_max":2},"<main>","insertSort\\insertSort.rb","top",[],0,[],[1,["putnil"],["putnil"],["defineclass","InsertionSort",["YARVInstructionSequence\/SimpleDataFormat",1,1,1,{"arg_size":0,"local_size":2,"stack_max":2},"<class:InsertionSort>","insertSort\\insertSort.rb","class",["msg"],0,[],[2,["putstring","Insertion Sort:"],["setlocal",2],3,["putnil"],["getlocal",2],["send","puts",1,null,8,null],["pop"],5,["putnil"],["definemethod","sort",["YARVInstructionSequence\/SimpleDataFormat",1,1,1,{"arg_size":0,"local_size":5,"stack_max":3},"sort","insertSort\\insertSort.rb","method",["ary","i","n","tmp"],0,[["break",null,"label_24","label_50","label_50",0]],[6,["duparray",[3,6,2,5,3,7,1,8]],["setlocal",5],7,["putnil"],["putstring","Before insertion sort"],["send","puts",1,null,8,null],["pop"],8,["putnil"],["getlocal",5],["send","puts",1,null,8,null],["pop"],"label_24",10,["putobject",1],["getlocal",5],["send","length",0,null,0,null],["putobject",1],["send","-",1,null,0,null],["newrange",0],["send","each",0,["YARVInstructionSequence\/SimpleDataFormat",1,1,1,{"arg_size":1,"local_size":1,"stack_max":5},"block in sort","insertSort\\insertSort.rb","block",[],[0,[],0,-1,0,-1,0],[["break",null,"label_14","label_181","label_181",0],["next",null,"label_14","label_181","label_128",0],["redo",null,"label_14","label_181","label_14",0],["redo",null,"label_8","label_181","label_8",0],["next",null,"label_8","label_181","label_181",0]],[20,["getdynamic",1,0],["expandarray",1,0],10,["setlocal",4],"label_8",11,["getlocal",4],["setlocal",3],12,["jump","label_128"],"label_14",13,["getlocal",5],["getlocal",3],["send","[]",1,null,0,null],["getlocal",5],["getlocal",3],["putobject",1],["send","-",1,null,0,null],["send","[]",1,null,0,null],["send","<",1,null,0,null],["branchunless","label_116"],["jump","label_52"],"label_52",14,["getlocal",5],["getlocal",3],["send","[]",1,null,0,null],["setlocal",2],15,["getlocal",5],["getlocal",3],["getlocal",5],["getlocal",3],["putobject",1],["send","-",1,null,0,null],["send","[]",1,null,0,null],["send","[]=",2,null,0,null],["pop"],16,["getlocal",5],["getlocal",3],["putobject",1],["send","-",1,null,0,null],["getlocal",2],["send","[]=",2,null,0,null],["pop"],13,["jump","label_116"],"label_116",18,["getlocal",3],["putobject",1],["send","-",1,null,0,null],["setlocal",3],"label_128",12,["getlocal",3],["putobject",1],["send",">=",1,null,0,null],["branchunless","label_180"],["jump","label_142"],"label_142",["getlocal",5],["getlocal",3],["send","[]",1,null,0,null],["getlocal",5],["getlocal",3],["putobject",1],["send","-",1,null,0,null],["send","[]",1,null,0,null],["send","<",1,null,0,null],["branchunless","label_180"],["jump","label_14"],"label_180",["putnil"],"label_181",["leave"]]],0,null],"label_50",["pop"],21,["putnil"],["putstring","After insertion sort"],["send","puts",1,null,8,null],["pop"],22,["putnil"],["getlocal",5],["send","puts",1,null,8,null],["leave"]]],0],["putnil"],["leave"]]],0],["pop"],26,["putnil"],["getconstant","InsertionSort"],["send","new",0,null,0,null],["send","sort",0,null,0,null],["leave"]]];
View
@@ -1 +1 @@
-["YARVInstructionSequence\/SimpleDataFormat",1,1,1,{"arg_size":0,"local_size":1,"stack_max":2},"<main>","t.rb","top",[],0,[],[1,["putnil"],["putnil"],["defineclass","C",["YARVInstructionSequence\/SimpleDataFormat",1,1,1,{"arg_size":0,"local_size":1,"stack_max":1},"<class:C>","t.rb","class",[],0,[],[2,["putnil"],["definemethod","m",["YARVInstructionSequence\/SimpleDataFormat",1,1,1,{"arg_size":0,"local_size":1,"stack_max":1},"m","t.rb","method",[],0,[["break",null,"label_0","label_8","label_8",0]],["label_0",3,["putobject",1],["send","times",0,["YARVInstructionSequence\/SimpleDataFormat",1,1,1,{"arg_size":0,"local_size":0,"stack_max":2},"block in m","t.rb","block",[],0,[["redo",null,"label_0","label_9","label_0",0],["next",null,"label_0","label_9","label_9",0]],["label_0",4,["putnil"],["putstring","Hello"],["send","print",1,null,8,null],"label_9",["leave"]]],0,null],"label_8",["leave"]]],0],["putnil"],["leave"]]],0],["pop"],9,["putnil"],["getconstant","C"],["send","new",0,null,0,null],["send","m",0,null,0,null],["leave"]]]
+["YARVInstructionSequence\/SimpleDataFormat",1,1,1,{"arg_size":0,"local_size":1,"stack_max":2},"<main>","t.rb","top",[],0,[],[1,["putnil"],["putnil"],["defineclass","C",["YARVInstructionSequence\/SimpleDataFormat",1,1,1,{"arg_size":0,"local_size":1,"stack_max":1},"<class:C>","t.rb","class",[],0,[],[2,["putnil"],["definemethod","m",["YARVInstructionSequence\/SimpleDataFormat",1,1,1,{"arg_size":0,"local_size":1,"stack_max":1},"m","t.rb","method",[],0,[["break",null,"label_0","label_8","label_8",0]],["label_0",3,["putobject",1],["send","times",0,["YARVInstructionSequence\/SimpleDataFormat",1,1,1,{"arg_size":0,"local_size":0,"stack_max":2},"block in m","t.rb","block",[],0,[["redo",null,"label_0","label_9","label_0",0],["next",null,"label_0","label_9","label_9",0]],["label_0",4,["putnil"],["putstring","Hello"],["send","puts",1,null,8,null],"label_9",["leave"]]],0,null],"label_8",["leave"]]],0],["putnil"],["leave"]]],0],["pop"],9,["putnil"],["getconstant","C"],["send","new",0,null,0,null],["send","m",0,null,0,null],["leave"]]]
View
@@ -1,7 +1,7 @@
class C
def m
1.times{
- print("Hello");
+ puts "Hello";
}
end
end
View
@@ -15,14 +15,25 @@ var HotRuby = function() {
* @type Array
*/
this.endBlocks = [];
+ /**
+ * Running Enviornment
+ * @type String
+ */
+ this.env = "browser";
+ this.nilObj = {
+ __className : "NilClass",
+ __parentClass : this.classes.Object
+ };
+ this.trueObj = {
+ __className : "TrueClass",
+ __parentClass : this.classes.Object
+ };
+ this.falseObj = {
+ __className : "FalseClass",
+ __parentClass : this.classes.Object
+ };
- this.supportRhino();
- if(!HotRuby.isRhino) {
- this.debugDom = document.getElementById("debug");
- if (this.debugDom == null) {
- this.debugDom = document.body;
- }
- }
+ this.checkEnv();
};
/**
@@ -114,8 +125,8 @@ HotRuby.prototype = {
* @private
*/
runOpcode : function(opcode, classObj, methodName, self, args, parentSF, isProc, cbaseObj) {
- if(args.length != opcode[4].arg_size)
- throw "wrong number of arguments (" + args.length + " for " + opcode[4].arg_size + ")";
+ if(args.length < opcode[4].arg_size)
+ throw "[runOpcode] Wrong number of arguments (" + args.length + " for " + opcode[4].arg_size + ")";
// Create Stack Frame
var sf = new HotRuby.StackFrame();
@@ -130,7 +141,7 @@ HotRuby.prototype = {
sf.cbaseObj = cbaseObj;
// Copy args to localVars. Fill from last.
- for (var i = 0;i < args.length; i++) {
+ for (var i = 0;i < opcode[4].arg_size; i++) {
sf.localVars[sf.localVars.length - 1 - i] = args[i];
}
@@ -185,26 +196,27 @@ HotRuby.prototype = {
if (!(cmd instanceof Array))
continue;
+ console.log("cmd = " + cmd[0] + ", sp = " + sf.sp);
switch (cmd[0]) {
case "jump" :
ip = opcode.label2ip[cmd[1]];
break;
case "branchif" :
var val = sf.stack[--sf.sp];
- if(val != null && val != false) {
+ if(val != this.nilObj && val != this.falseObj) {
ip = opcode.label2ip[cmd[1]];
}
break;
case "branchunless" :
var val = sf.stack[--sf.sp];
- if(val == null || val == false) {
+ if(val == this.nilObj || val == this.falseObj) {
ip = opcode.label2ip[cmd[1]];
}
break;
case "leave" :
return;
case "putnil" :
- sf.stack[sf.sp++] = null;
+ sf.stack[sf.sp++] = this.nilObj;
break;
case "putself" :
sf.stack[sf.sp++] = sf.self;
@@ -230,17 +242,17 @@ HotRuby.prototype = {
break;
case "newarray" :
var value = this.createRubyArray(sf.stack.slice(sf.sp - cmd[1], sf.sp));
- sf.sp -= value.length;
+ sf.sp -= value.__native.length;
sf.stack[sf.sp++] = value;
break;
case "duparray" :
sf.stack[sf.sp++] = this.createRubyArray(cmd[1]);
break;
case "expandarray" :
var ary = sf.stack[--sf.sp];
- if(ary instanceof Array) {
+ if(typeof(ary) == "object" && ary.__className == "Array") {
for(var i=0; i<cmd[1]; i++) {
- sf.stack[sf.sp++] = ary[i];
+ sf.stack[sf.sp++] = ary.__native[i];
}
if(cmd[2] && 1) {
// TODO
@@ -254,7 +266,7 @@ HotRuby.prototype = {
} else {
sf.stack[sf.sp++] = ary;
for (var i = 0;i < cmd[1] - 1; i++) {
- sf.stack[sf.sp++] = null;
+ sf.stack[sf.sp++] = this.nilObj;
}
}
break;
@@ -366,7 +378,7 @@ HotRuby.prototype = {
var args = sf.stack.slice(sf.sp - cmd[2], sf.sp);
sf.sp -= cmd[2];
var recver = sf.stack[--sf.sp];
- if (recver == null)
+ if (recver == null || recver == this.nilObj)
recver = sf.self;
if(cmd[3] instanceof Array)
cmd[3] = this.createRubyProc(cmd[3], sf);
@@ -389,10 +401,11 @@ HotRuby.prototype = {
var obj = sf.stack[--sf.sp];
if(sf.cbaseObj != null)
obj = sf.cbaseObj;
- if (obj == null) {
+ if (obj == null || obj == this.nilObj) {
sf.classObj[cmd[1]] = cmd[2];
} else {
if (!("__methods" in obj))
+ //if(typeof(obj.__methods) == "undefined")
obj.__methods = {};
obj.__methods[cmd[1]] = cmd[2];
}
@@ -401,8 +414,8 @@ HotRuby.prototype = {
break;
case "defineclass" :
var parentClass = sf.stack[--sf.sp];
- var isRedefine = (parentClass == false);
- if(parentClass == null)
+ var isRedefine = (parentClass == this.falseObj);
+ if(parentClass == null || parentClass == this.nilObj)
parentClass = this.classes.Object;
var cbaseObj = sf.stack[--sf.sp];
if(cmd[3] == 0) {
@@ -474,7 +487,8 @@ HotRuby.prototype = {
}
} else {
// Search method in object
- if (recver != null && recver.__methods != null) {
+ //if (recver != null && recver.__methods != null) {
+ if (recver != null && typeof(recver) == "object" && "__methods" in recver) {
func = recver.__methods[methodName];
}
if (func == null) {
@@ -530,7 +544,7 @@ HotRuby.prototype = {
// Splat array args
if (type & HotRuby.VM_CALL_ARGS_SPLAT_BIT) {
- args = args.concat(args.pop());
+ args = args.concat(args.pop().__native);
}
// Exec method
@@ -561,9 +575,9 @@ HotRuby.prototype = {
* @private
*/
setConstant : function(sf, classObj, constName, constValue) {
- if (classObj == null) {
+ if (classObj == null || classObj == this.nilObj) {
classObj = sf.classObj;
- } else if (classObj == false) {
+ } else if (classObj == false || classObj == this.falseObj) {
// TODO
throw "[setConstant] Not implemented";
}
@@ -582,7 +596,7 @@ HotRuby.prototype = {
* @private
*/
getConstant : function(sf, classObj, constName) {
- if (classObj == null) {
+ if (classObj == null || classObj == this.nilObj) {
var isFound = false;
// Search outer(parentStackFrame)
for (var checkSF = sf;!isFound; checkSF = checkSF.parentStackFrame) {
@@ -608,11 +622,11 @@ HotRuby.prototype = {
if (!isFound) {
classObj = this.classes.Object;
}
- } else if (classObj == false) {
+ } else if (classObj == false || classObj == this.falseObj) {
// TODO
throw "[setConstant] Not implemented";
}
- if (classObj == null)
+ if (classObj == null || classObj == this.nilObj)
throw "[getConstant] Cannot find constant : " + constName;
// Const in <global> belongs to Object
if (classObj.__className == "<global>")
@@ -648,9 +662,11 @@ HotRuby.prototype = {
* @return {String}
*/
createRubyString : function(str) {
- str.__className = "String";
- str.__parentClass = this.classes.Object;
- return str;
+ return {
+ __native : str,
+ __className : "String",
+ __parentClass : this.classes.Object
+ };
},
/**
@@ -674,9 +690,11 @@ HotRuby.prototype = {
* @return {Array}
*/
createRubyArray : function(ary) {
- ary.__className = "Array";
- ary.__parentClass = this.classes.Object;
- return ary;
+ return {
+ __native : ary,
+ __className : "Array",
+ __parentClass : this.classes.Object
+ };
},
/**
@@ -690,10 +708,15 @@ HotRuby.prototype = {
__parentClass : this.classes.Object,
__instanceVars : {
length : ary.length / 2
- }
+ },
+ __native : {}
};
for (var i = 0;i < ary.length; i += 2) {
- hash[ary[i]] = ary[i + 1];
+ if(typeof(ary[i]) == "object" && ary[i].__className == "String") {
+ hash.__native[ary[i].__native] = ary[i + 1];
+ } else {
+ throw "[createRubyHash] Unsupported. Cannot put this object to Hash";
+ }
}
return hash;
},
@@ -711,7 +734,7 @@ HotRuby.prototype = {
__instanceVars : {
first : first,
last : last,
- exclude_end : exclude_end
+ exclude_end : exclude_end ? this.trueObj : this.falseObj
}
};
},
@@ -721,13 +744,19 @@ HotRuby.prototype = {
* @param {String} str
*/
printDebug : function(str) {
- if(HotRuby.isRhino) {
- print(str);
- } else {
- var div = document.createElement("div");
- var text = document.createTextNode(str);
- div.appendChild(text);
- this.debugDom.appendChild(div);
+ switch(HotRuby.env) {
+ case "browser":
+ var div = document.createElement("div");
+ var text = document.createTextNode(str);
+ div.appendChild(text);
+ this.debugDom.appendChild(div);
+ break;
+ case "flash":
+ HotRuby.debugTextField.text += str + "\n";
+ break;
+ case "rhino":
+ print(str);
+ break;
}
},
@@ -772,12 +801,26 @@ HotRuby.prototype = {
);
},
- supportRhino : function() {
- HotRuby.isRhino = (typeof(alert) == "undefined");
- if (HotRuby.isRhino) {
+ checkEnv : function() {
+ if(typeof(_root) != "undefined") {
+ HotRuby.env = "flash";
+ HotRuby.debugTextField = new TextField();
+ HotRuby.debugTextField.autoSize = TextFieldAutoSize.LEFT;
+ _root.addChild(HotRuby.debugTextField);
+ alert = function(str) {
+ HotRuby.debugTextField.text += str + "\n";
+ }
+ } else if(typeof(alert) == "undefined") {
+ HotRuby.env = "rhino";
alert = function(str) {
print(str);
}
+ } else {
+ HotRuby.env = "browser";
+ this.debugDom = document.getElementById("debug");
+ if (this.debugDom == null) {
+ this.debugDom = document.body;
+ }
}
}
};
Oops, something went wrong.

0 comments on commit db51557

Please sign in to comment.