Skip to content

Instantly share code, notes, and snippets.

@jasononeil
Created June 12, 2016 12:24
Show Gist options
  • Save jasononeil/bf5da8e176e595f476720ffffa6816b9 to your computer and use it in GitHub Desktop.
Save jasononeil/bf5da8e176e595f476720ffffa6816b9 to your computer and use it in GitHub Desktop.
Demonstrate how much extra code reflection (Reflect.* and Type.*) generate in Haxe code
class ReflectionCost {
public static function main() {
var obj = new ReflectionCost();
#if reflect
Reflect.setField( obj, "name", "Jason" );
Reflect.callMethod( obj, Reflect.field(obj, "printName"), [] );
Type.getInstanceFields( Type.getClass(obj) );
#else
obj.name = "Jason";
obj.printName();
["name"];
#end
}
public var name:String;
public function new() {}
public function printName() trace( name );
}
-main ReflectionCost
-js ReflectionCost.no.reflection.js
--next
-main ReflectionCost
-D reflect
-js ReflectionCost.reflection.js
(function (console) { "use strict";
var ReflectionCost = function() {
};
ReflectionCost.main = function() {
var obj = new ReflectionCost();
obj.name = "Jason";
obj.printName();
["name"];
};
ReflectionCost.prototype = {
printName: function() {
console.log(this.name);
}
};
ReflectionCost.main();
})(typeof console != "undefined" ? console : {log:function(){}});
(function (console, $global) { "use strict";
var HxOverrides = function() { };
HxOverrides.indexOf = function(a,obj,i) {
var len = a.length;
if(i < 0) {
i += len;
if(i < 0) i = 0;
}
while(i < len) {
if(a[i] === obj) return i;
i++;
}
return -1;
};
HxOverrides.remove = function(a,obj) {
var i = HxOverrides.indexOf(a,obj,0);
if(i == -1) return false;
a.splice(i,1);
return true;
};
var Reflect = function() { };
Reflect.field = function(o,field) {
try {
return o[field];
} catch( e ) {
return null;
}
};
Reflect.callMethod = function(o,func,args) {
return func.apply(o,args);
};
var ReflectionCost = function() {
};
ReflectionCost.main = function() {
var obj = new ReflectionCost();
obj.name = "Jason";
Reflect.callMethod(obj,Reflect.field(obj,"printName"),[]);
Type.getInstanceFields(obj == null?null:js_Boot.getClass(obj));
};
ReflectionCost.prototype = {
name: null
,printName: function() {
console.log(this.name);
}
,__class__: ReflectionCost
};
var Type = function() { };
Type.getInstanceFields = function(c) {
var a = [];
for(var i in c.prototype) a.push(i);
HxOverrides.remove(a,"__class__");
HxOverrides.remove(a,"__properties__");
return a;
};
var js_Boot = function() { };
js_Boot.getClass = function(o) {
if((o instanceof Array) && o.__enum__ == null) return Array; else {
var cl = o.__class__;
if(cl != null) return cl;
var name = js_Boot.__nativeClassName(o);
if(name != null) return js_Boot.__resolveNativeClass(name);
return null;
}
};
js_Boot.__nativeClassName = function(o) {
var name = js_Boot.__toStr.call(o).slice(8,-1);
if(name == "Object" || name == "Function" || name == "Math" || name == "JSON") return null;
return name;
};
js_Boot.__resolveNativeClass = function(name) {
return $global[name];
};
if(Array.prototype.indexOf) HxOverrides.indexOf = function(a,o,i) {
return Array.prototype.indexOf.call(a,o,i);
};
String.prototype.__class__ = String;
js_Boot.__toStr = {}.toString;
ReflectionCost.main();
})(typeof console != "undefined" ? console : {log:function(){}}, typeof window != "undefined" ? window : typeof global != "undefined" ? global : typeof self != "undefined" ? self : this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment