From 555b83a359f7c606f53cdcb2e1ee65e702ee460d Mon Sep 17 00:00:00 2001 From: Tom Robinson Date: Thu, 25 Feb 2010 00:56:54 -0800 Subject: [PATCH] "flatten" now inlines functions --- Objective-J/FileExecutable.js | 7 +++++-- Tools/flatten/flatten | 33 +++++++++++++++++++++++++++------ 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/Objective-J/FileExecutable.js b/Objective-J/FileExecutable.js index 9f5fe6d59..a2e151791 100644 --- a/Objective-J/FileExecutable.js +++ b/Objective-J/FileExecutable.js @@ -22,7 +22,7 @@ var FileExecutablesForPaths = { }; -function FileExecutable(/*String*/ aPath) +function FileExecutable(/*String*/ aPath, /*Executable*/ anExecutable) { var existingFileExecutable = FileExecutablesForPaths[aPath]; @@ -35,7 +35,10 @@ function FileExecutable(/*String*/ aPath) executable = NULL, extension = FILE.extension(aPath); - if (fileContents.match(/^@STATIC;/)) + if (anExecutable) + executable = anExecutable; + + else if (fileContents.match(/^@STATIC;/)) executable = decompile(fileContents, aPath); else if (extension === ".j" || extension === "") diff --git a/Tools/flatten/flatten b/Tools/flatten/flatten index 9854df81a..6889a931d 100755 --- a/Tools/flatten/flatten +++ b/Tools/flatten/flatten @@ -31,7 +31,7 @@ function main(args) var flattener = new ObjectiveJFlattener(rootPath); - flattener.setIncludePaths([rootPath.join("Frameworks/Debug")]); + flattener.setIncludePaths([rootPath.join("Frameworks")]); flattener.setEnvironments(["Browser", "ObjJ"]); flattener.load(rootPath.join("main.j")); @@ -65,6 +65,7 @@ function ObjectiveJFlattener(rootPath) { this.resourceBuffer = []; this.bundleBuffer = []; + this.functionsBuffer = []; this._outputBundles = {}; } @@ -75,6 +76,7 @@ ObjectiveJFlattener.prototype.buildApplicationJS = function(applicationRoot) { print(applicationRoot); this.serializeStaticResources(applicationRoot); + this.serializeStaticFileExecutables(); var buffer = [] @@ -89,14 +91,31 @@ ObjectiveJFlattener.prototype.buildApplicationJS = function(applicationRoot) { buffer.push(this.resourceBuffer.join("\n")); - buffer.push("console.log(ObjectiveJ.StaticResource.root);"); - buffer.push("console.log(applicationRoot);"); - buffer.push("ObjectiveJ.bootstrap();"); buffer.push("})();"); + buffer.push(this.functionsBuffer.join("\n")); + + buffer.push("ObjectiveJ.bootstrap();"); + return buffer.join("\n"); } +ObjectiveJFlattener.prototype.serializeStaticFileExecutables = function() { + this.require("objective-j").FileExecutable.allFileExecutables().forEach(function(aFileExecutable) { + var deps = aFileExecutable.fileDependencies().map(function(dependency) { + return "new ObjectiveJ.FileDependency("+JSON.stringify(dependency.path())+","+JSON.stringify(dependency.isLocal())+")" + }).join(","); + + var func = aFileExecutable._function.toString(); + // HACK + func = func.replace(", require, exports, module, system, print, window", ""); + + this.functionsBuffer.push("var path = ObjectiveJ.StaticResource.cwd + "+JSON.stringify("/"+this.rootPath.relative(aFileExecutable.path()))+";"); + this.functionsBuffer.push("new ObjectiveJ.FileExecutable(path,"+ + "new ObjectiveJ.Executable(null, ["+deps+"], path, "+func+"));"); + }, this); +} + ObjectiveJFlattener.prototype.serializeStaticResources = function(node, depth) { depth = depth || 0; @@ -130,8 +149,10 @@ ObjectiveJFlattener.prototype.serializeStaticResources = function(node, depth) { this.resourceBuffer.push("newNode = applicationRoot;"); } - if (node.contents()) - this.resourceBuffer.push("newNode._contents = " + JSON.stringify(node.contents()) + ";"); + // var contents = node.contents(); + // if (contents) { + // this.resourceBuffer.push("newNode._contents = " + JSON.stringify(contents) + ";"); + // } if (!node.children()) return;