diff cbackend.js @ 54:976a0924e1d4

Fix closure over self var
author Mike Pavone <pavone@retrodev.com>
date Fri, 13 Jul 2012 19:22:39 -0700
parents ab6217b8ae4c
children 93ddb4ad6fcb
line wrap: on
line diff
--- a/cbackend.js	Fri Jul 13 18:31:32 2012 -0700
+++ b/cbackend.js	Fri Jul 13 19:22:39 2012 -0700
@@ -55,12 +55,13 @@
 	return name;
 }
 
-function getSymbolPrefix(info)
+function getSymbolPrefix(info, symbols)
 {
 	var pre = '';
 	switch(info.type) {
 	case 'self':
-		pre = 'self->';
+		
+		pre = (new symbol('self', symbols)).toC() + '->';
 		break;
 	case 'parent':
 		pre = 'self->';
@@ -96,7 +97,7 @@
 	if (info.type == 'toplevel') {
 		return info.def.modulevar;
 	}
-	return getSymbolPrefix(info) + escapeCName(name);
+	return getSymbolPrefix(info, this.symbols) + escapeCName(name);
 }
 
 var declaredInts = {};
@@ -253,7 +254,7 @@
 	for (var file in this.includes) {
 		includes += '#include ' + file + '\n';
 	}
-	var objdef =  'typedef struct {\n\tobject header;\n';
+	var objdef =  'typedef struct ' + this.name + ' {\n\tobject header;\n';
 	for (var i in this.properties) {
 		if (this.properties[i] instanceof Array) {
 			objdef += '\t' + this.properties[i][1] + ' ' + this.properties[i][0] + ';\n';
@@ -680,8 +681,10 @@
 		toplevelcode += builtins[i].toCDef();
 	}
 	addBuiltinModules(toplevel);
+	debugprint('//------POPULATING SYMBOLS-----');
 	obj.populateSymbols(toplevel);
 	var moduleinit = processUsedToplevel(toplevel);
+	debugprint('//------COMPILING AST-----');
 	var rest = 'object * mainModule() {\n' + moduleinit + '\tmain_module = ' + obj.toC() + ';\n\treturn main_module;\n}\n';
 	return '#include "runtime/proghead.inc"\n' +
 		'#define METHOD_ID_MAIN ' + getMethodId('main') + '\n' +
@@ -732,7 +735,11 @@
 	if (Object.keys(this.symbols.closedover).length) {
 		forwarddec += 'typedef struct lambda_' + mynum + '_env {\n';
 		for (var varname in this.symbols.closedover) {
-			forwarddec += '\tobject * ' + escapeCName(varname) + ';\n';
+			if (varname == 'self' && this.selftype) {
+				forwarddec += '\tstruct ' + this.selftype + ' * self;\n';
+			} else {
+				forwarddec += '\tobject * ' + escapeCName(varname) + ';\n';
+			}
 		}
 		forwarddec += '} lambda_' + mynum + '_env;\n'
 		
@@ -779,6 +786,7 @@
 }
 
 assignment.prototype.toC = function() {
+	debugprint('//assignment', this.symbol.name);
 	var existing = this.symbols.find(this.symbol.name);
 	var prefix = '';
 	var val = this.expression.toC();
@@ -788,10 +796,12 @@
 	if (existing.type == 'local' && !existing.isdeclared) {
 		prefix = 'object *';
 		this.symbols.declareVar(this.symbol.name);
+		debugprint('//declared var', this.symbol.name);
 	}
 	return prefix + this.symbol.toC() + ' = ' + val;
 };
 assignment.prototype.toCObject = function(cobj) {
+	debugprint('//message definition', this.symbol.name);
 	if (this.expression.toCObject) {
 		var val = this.expression.toCObject(cobj.name);
 	} else {