Skip to content
Browse files

Updates

  • Loading branch information...
1 parent e571b2e commit 4ffade9c0dde5c62bd8c11fb298ab8a0129cea8c @codeboost committed May 21, 2011
Showing with 112 additions and 4 deletions.
  1. +14 −2 beautils.js
  2. +37 −1 classconvert.js
  3. +11 −1 src/beautils.coffee
  4. +50 −0 src/classconvert.coffee
View
16 beautils.js
@@ -101,14 +101,19 @@
return Argument;
})();
parseDeclaration = function(str, namespace) {
- var arg, args, argsEnd, argsStart, decla, fnArgs, fnDec, parseArgs, _i, _len;
+ var arg, args, argsEnd, argsStart, decla, fnArgs, fnDec, isPure, isVirtual, parseArgs, _i, _len;
argsStart = str.indexOf('(');
argsEnd = str.lastIndexOf(')');
if (argsStart === -1 || argsEnd === -1) {
return false;
}
args = str.slice(argsStart + 1, argsEnd);
decla = str.slice(0, argsStart);
+ isPure = false;
+ if (/\s*=\s*0/.test(decla)) {
+ isPure = true;
+ decla = decla.replace(/\s*=\s*0;*/, '');
+ }
parseArgs = function(args) {
var char, cur, paran, ret, symbol, _i, _len;
if (args.length === 0) {
@@ -153,9 +158,16 @@
arg = args[_i];
fnArgs.push(new Argument(arg, namespace));
}
+ isVirtual = false;
+ if (/^virtual\s+/.test(decla)) {
+ decla = decla.replace(/^virtual\s+/, '');
+ isVirtual = true;
+ }
fnDec = new Argument(decla, namespace);
return _.extend(fnDec, {
- args: fnArgs
+ args: fnArgs,
+ virtual: isVirtual,
+ pure: isPure
});
};
isSameOverload = function(overload1, overload2) {
View
38 classconvert.js
@@ -35,6 +35,7 @@
this.classFns = {};
this.className = "";
this.nativeClassName = "";
+ this.virtual = false;
this.exposed = true;
this.namespace = '';
this.nsBlock = null;
@@ -49,7 +50,7 @@
return false;
};
ClassConverter.prototype.processClass = function(cl, targetNamespace) {
- var declaNs, ret, _ref;
+ var declaNs, derivedDecla, ret, _ref;
this.namespace = cl.namespace;
if (/^@static\s+/.test(cl.node.text)) {
this.isStatic = true;
@@ -64,6 +65,9 @@
_.each(cl.node.children, __bind(function(child) {
return this.processFunNode(child);
}, this));
+ if (this.virtual) {
+ derivedDecla = this.createDerivedClass();
+ }
this.globalBlock = new CodeBlock.CodeBlock;
if (!this.options.manual) {
if (!this.isStatic) {
@@ -142,6 +146,7 @@
return this.warn("Cannot parse method declaration: '" + str + "'. Ignoring.", node);
}
if (fn.type.rawType === this.nativeClassName && fn.name === "") {
+ fn.orgName = fn.name;
fn.name = '__constructor';
}
if (isManual) {
@@ -152,6 +157,9 @@
fn.requiredArgs = this.requiredArgs(fn.args);
fn.sublines = node.children;
fn.node = node;
+ if (fn.virtual) {
+ this.virtual = true;
+ }
callNode = _.detect(fn.sublines, function(subline) {
return /^\@call/.test(subline.text);
});
@@ -234,6 +242,34 @@
decBlock.add(new CodeBlock.CodeBlock("public:", false)).add("static void _InitJSObject(v8::Handle<v8::Object> target);");
return decBlock;
};
+ ClassConverter.prototype.createDerivedClass = function() {
+ var classBlock, constructors, public, vfuncs;
+ classBlock = new CodeBlock.ClassBlock("class bea_" + this.classType + " : public " + this.classType + ", public bea::DerivedClass");
+ public = classBlock.add(new CodeBlock.CodeBlock("public:", false));
+ constructors = _.detect(this.classFns, function(fn) {
+ return fn.name === '__constructor';
+ });
+ _.each(constructors, function(constr) {
+ var cargs, dargs;
+ dargs = _.map(constr.args, arg(arg.org));
+ cargs = _.map(constr.args, arg(arg.name));
+ return public.add("bea_" + this.classType + "(" + (dargs.join(', ')) + ") : " + this.classType + "(" + (cargs.join(', ')) + "){}");
+ });
+ vfuncs = _.select(this.classFns, function(fn) {
+ return fn.virtual;
+ });
+ public.add("//Virtual functions");
+ return _.each(vfuncs, function(vfunc) {
+ var cargs, dargs, ret;
+ dargs = _.map(vfunc.args, arg(arg.org));
+ cargs = _.map(vfun.args, arg(arg.name));
+ ret = 'return';
+ if (vfunc.type.rawType === 'void') {
+ ret = '';
+ }
+ return public.add("inline " + (vfunc.type.fullType()) + " bea_" + vfunc.name + "(" + (dargs.join(', ')) + "){" + ret + " " + this.classType + "::" + vfunc.name + "(" + (cargs.join(', ')) + ");}");
+ });
+ };
ClassConverter.prototype.createInitFn = function() {
var initFn;
initFn = new CodeBlock.FunctionBlock(snippets.decl.InitJSObject(this.className));
View
12 src/beautils.coffee 100644 → 100755
@@ -104,6 +104,11 @@ parseDeclaration = (str, namespace) ->
args = str.slice argsStart + 1, argsEnd
decla = str.slice(0, argsStart);
+ isPure = false
+ if /\s*=\s*0/.test decla
+ isPure = true
+ decla = decla.replace /\s*=\s*0;*/, ''
+
parseArgs = (args) ->
if args.length == 0 then return []
ret = []
@@ -141,8 +146,13 @@ parseDeclaration = (str, namespace) ->
fnArgs.push new Argument(arg, namespace) for arg in args
+ isVirtual = false
+ if /^virtual\s+/.test decla
+ decla = decla.replace /^virtual\s+/, ''
+ isVirtual = true
+
fnDec = new Argument decla, namespace
- _.extend fnDec, {args: fnArgs}
+ _.extend fnDec, {args: fnArgs, virtual: isVirtual, pure: isPure}
isSameOverload = (overload1, overload2) ->
overload1.name == overload2.name &&
View
50 src/classconvert.coffee
@@ -22,6 +22,7 @@ class ClassConverter
@classFns = {}
@className = ""
@nativeClassName = ""
+ @virtual = false #has virtual functions
@exposed = true
@namespace = ''
@nsBlock = null
@@ -53,6 +54,15 @@ class ClassConverter
#parse all functions
_.each cl.node.children, (child) => @processFunNode child
+ #has virtual functions?
+ if @virtual
+ #generate derived class block
+ #change className to bea_className
+ #function call will be _this->bea_functionName(arguments)
+ derivedDecla = @createDerivedClass();
+
+
+
@globalBlock = new CodeBlock.CodeBlock
if not @options.manual
@@ -132,7 +142,9 @@ class ClassConverter
if not fn then return @warn "Cannot parse method declaration: '#{str}'. Ignoring.", node
if fn.type.rawType == @nativeClassName && fn.name == ""
+ fn.orgName = fn.name
fn.name = '__constructor'
+
if isManual then @logger.stats.manual++
@@ -142,6 +154,8 @@ class ClassConverter
fn.sublines = node.children
fn.node = node
+ if fn.virtual then @virtual = true
+
#check sublines for @call directive
callNode = _.detect fn.sublines, (subline) -> /^\@call/.test subline.text
@@ -218,6 +232,42 @@ class ClassConverter
return decBlock
+ createDerivedClass: ->
+
+ classBlock = new CodeBlock.ClassBlock "class bea_#{@classType} : public #{@classType}, public bea::DerivedClass"
+
+ public = classBlock.add (new CodeBlock.CodeBlock "public:", false)
+
+ #constructor
+ constructors = _.detect @classFns, (fn) -> fn.name == '__constructor'
+
+ _.each constructors, (constr) ->
+
+ #declaration arguments
+ dargs = _.map constr.args, (arg) arg.org
+ #call arguments
+ cargs = _.map constr.args, (arg) arg.name
+
+ #bea_Derived() : Derived(){}
+ #bea_Derived(int k, CClass* ptr): Derived(k, ptr){}
+ public.add "bea_#{@classType}(#{dargs.join ', '}) : #{@classType}(#{cargs.join(', ')}){}"
+
+ #add virtual functions
+
+ vfuncs = _.select @classFns, (fn) -> fn.virtual
+
+ public.add "//Virtual functions"
+ _.each vfuncs, (vfunc) ->
+ dargs = _.map vfunc.args, (arg) arg.org
+ cargs = _.map vfun.args, (arg) arg.name
+
+ ret = 'return'
+ if vfunc.type.rawType == 'void' then ret = ''
+
+ public.add "inline #{vfunc.type.fullType()} bea_#{vfunc.name}(#{dargs.join ', '}){#{ret} #{@classType}::#{vfunc.name}(#{cargs.join ', '});}"
+
+
+
#Create the InitJSObject function, to be added to the CPP file.
#This function will be called by the exposing function
createInitFn: ->

0 comments on commit 4ffade9

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