From 3bbcc1efd8c2d499c9ec6d8ed2093c48fdee61e7 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 3 Feb 2015 15:56:58 -0800 Subject: [PATCH 1/7] compile-coffee.py should be put under tools/ --- atom.gyp | 6 +++--- {script => tools}/compile-coffee.py | 0 2 files changed, 3 insertions(+), 3 deletions(-) rename {script => tools}/compile-coffee.py (100%) diff --git a/atom.gyp b/atom.gyp index 87188a7049..51b9b2edf7 100644 --- a/atom.gyp +++ b/atom.gyp @@ -622,7 +622,7 @@ 'rule_name': 'coffee', 'extension': 'coffee', 'inputs': [ - 'script/compile-coffee.py', + 'tools/compile-coffee.py', ], 'conditions': [ ['OS=="mac"', { @@ -631,7 +631,7 @@ ], 'action': [ 'python', - 'script/compile-coffee.py', + 'tools/compile-coffee.py', '<(RULE_INPUT_PATH)', '<(PRODUCT_DIR)/<(product_name).app/Contents/Resources/<(RULE_INPUT_DIRNAME)/<(RULE_INPUT_ROOT).js', ], @@ -641,7 +641,7 @@ ], 'action': [ 'python', - 'script/compile-coffee.py', + 'tools/compile-coffee.py', '<(RULE_INPUT_PATH)', '<(PRODUCT_DIR)/resources/<(RULE_INPUT_DIRNAME)/<(RULE_INPUT_ROOT).js', ], diff --git a/script/compile-coffee.py b/tools/compile-coffee.py similarity index 100% rename from script/compile-coffee.py rename to tools/compile-coffee.py From 8612ad0459b19a24969890f5de0a0b17395c3ed0 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 3 Feb 2015 16:07:29 -0800 Subject: [PATCH 2/7] generate_sources => compile_coffee --- atom.gyp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/atom.gyp b/atom.gyp index 51b9b2edf7..0a3928cac1 100644 --- a/atom.gyp +++ b/atom.gyp @@ -386,7 +386,7 @@ 'target_name': '<(project_name)', 'type': 'executable', 'dependencies': [ - 'generated_sources', + 'compile_coffee', '<(project_name)_lib', ], 'sources': [ @@ -612,7 +612,7 @@ ], }, # target <(product_name)_lib { - 'target_name': 'generated_sources', + 'target_name': 'compile_coffee', 'type': 'none', 'sources': [ '<@(coffee_sources)', @@ -649,7 +649,7 @@ ], }, ], - }, # target generated_sources + }, # target compile_coffee { 'target_name': '<(project_name)_dump_symbols', 'type': 'none', From f905bb64f66090000bbbee9f88e91e93637500d1 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 3 Feb 2015 16:46:52 -0800 Subject: [PATCH 3/7] Compile asar.coffee to .c --- atom.gyp | 29 ++++++++++++++++++++++++++++- tools/coffee2c.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100755 tools/coffee2c.py diff --git a/atom.gyp b/atom.gyp index 0a3928cac1..b235439a06 100644 --- a/atom.gyp +++ b/atom.gyp @@ -41,7 +41,6 @@ 'atom/common/api/lib/original-fs.coffee', 'atom/common/api/lib/shell.coffee', 'atom/common/lib/init.coffee', - 'atom/common/lib/asar.coffee', 'atom/renderer/lib/chrome-api.coffee', 'atom/renderer/lib/init.coffee', 'atom/renderer/lib/inspector.coffee', @@ -55,6 +54,9 @@ 'atom/renderer/api/lib/screen.coffee', 'atom/renderer/api/lib/web-frame.coffee', ], + 'coffee2c_sources': [ + 'atom/common/lib/asar.coffee', + ], 'lib_sources': [ 'atom/app/atom_content_client.cc', 'atom/app/atom_content_client.h', @@ -333,6 +335,7 @@ 'chromium_src/library_loaders/libspeechd_loader.cc', 'chromium_src/library_loaders/libspeechd.h', '<@(native_mate_files)', + '<(SHARED_INTERMEDIATE_DIR)/atom_natives.h', ], 'lib_sources_win': [ 'chromium_src/chrome/browser/ui/views/color_chooser_dialog.cc', @@ -524,6 +527,7 @@ 'target_name': '<(project_name)_lib', 'type': 'static_library', 'dependencies': [ + 'atom_coffee2c', 'vendor/brightray/brightray.gyp:brightray', 'vendor/node/node.gyp:node_lib', ], @@ -542,6 +546,8 @@ 'chromium_src', 'vendor/brightray', 'vendor/native_mate', + # Include atom_natives.h. + '<(SHARED_INTERMEDIATE_DIR)', # Include directories for uv and node. 'vendor/node/src', 'vendor/node/deps/http_parser', @@ -650,6 +656,27 @@ }, ], }, # target compile_coffee + { + 'target_name': 'atom_coffee2c', + 'type': 'none', + 'actions': [ + { + 'action_name': 'atom_coffee2c', + 'inputs': [ + '<@(coffee2c_sources)', + ], + 'outputs': [ + '<(SHARED_INTERMEDIATE_DIR)/atom_natives.h', + ], + 'action': [ + 'python', + 'tools/coffee2c.py', + '<@(_outputs)', + '<@(_inputs)', + ], + } + ], + }, # target atom_coffee2c { 'target_name': '<(project_name)_dump_symbols', 'type': 'none', diff --git a/tools/coffee2c.py b/tools/coffee2c.py new file mode 100755 index 0000000000..6c1241eb18 --- /dev/null +++ b/tools/coffee2c.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python + +import os +import subprocess +import sys + + +SOURCE_ROOT = os.path.dirname(os.path.dirname(__file__)) + + +def main(): + natives = sys.argv[1] + coffee_source_files = sys.argv[2:] + + output_dir = os.path.dirname(natives) + js_source_files = compile_coffee(coffee_source_files, output_dir) + call_js2c(natives, js_source_files) + + +def compile_coffee(coffee_source_files, output_dir): + js_source_files = [] + for source_file in coffee_source_files: + output_filename = os.path.splitext(source_file)[0] + '.js' + output_path = os.path.join(output_dir, output_filename) + js_source_files.append(output_path) + call_compile_coffee(source_file, output_path) + return js_source_files + + +def call_compile_coffee(source_file, output_filename): + compile_coffee = os.path.join(SOURCE_ROOT, 'tools', 'compile-coffee.py') + subprocess.check_call([sys.executable, compile_coffee, source_file, + output_filename]) + + +def call_js2c(natives, js_source_files): + js2c = os.path.join(SOURCE_ROOT, 'vendor', 'node', 'tools', 'js2c.py') + subprocess.check_call([sys.executable, js2c, natives] + js_source_files) + + +if __name__ == '__main__': + sys.exit(main()) From 45e2dd2ab56f2824b451e68e33a77ab859a622e7 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 4 Feb 2015 10:45:17 -0800 Subject: [PATCH 4/7] Init asar support without external .js files --- atom.gyp | 1 + atom/common/api/atom_api_asar.cc | 25 +++++++++++++++++++++++++ atom/common/lib/asar.coffee | 2 +- atom/common/lib/asar_init.coffee | 22 ++++++++++++++++++++++ atom/common/lib/init.coffee | 18 ------------------ vendor/node | 2 +- 6 files changed, 50 insertions(+), 20 deletions(-) create mode 100644 atom/common/lib/asar_init.coffee diff --git a/atom.gyp b/atom.gyp index b235439a06..7fb5337453 100644 --- a/atom.gyp +++ b/atom.gyp @@ -56,6 +56,7 @@ ], 'coffee2c_sources': [ 'atom/common/lib/asar.coffee', + 'atom/common/lib/asar_init.coffee', ], 'lib_sources': [ 'atom/app/atom_content_client.cc', diff --git a/atom/common/api/atom_api_asar.cc b/atom/common/api/atom_api_asar.cc index 03efa10a1e..5d35052b71 100644 --- a/atom/common/api/atom_api_asar.cc +++ b/atom/common/api/atom_api_asar.cc @@ -4,9 +4,11 @@ #include +#include "atom_natives.h" #include "atom/common/asar/archive.h" #include "atom/common/native_mate_converters/file_path_converter.h" #include "native_mate/arguments.h" +#include "native_mate/callback.h" #include "native_mate/dictionary.h" #include "native_mate/object_template_builder.h" #include "native_mate/wrappable.h" @@ -106,10 +108,33 @@ class Archive : public mate::Wrappable { DISALLOW_COPY_AND_ASSIGN(Archive); }; +void InitAsarSupport(v8::Isolate* isolate, + v8::Handle process, + v8::Handle require) { + // Evaluate asar_init.coffee. + v8::Local asar_init = v8::Script::Compile(v8::String::NewFromUtf8( + isolate, + node::asar_init_native, + v8::String::kNormalString, + sizeof(node::asar_init_native) -1)); + v8::Local result = asar_init->Run(); + + // Initialize asar support. + base::Callback, + v8::Handle, + std::string)> init; + if (mate::ConvertFromV8(isolate, result, &init)) { + init.Run(process, + require, + std::string(node::asar_native, sizeof(node::asar_native) - 1)); + } +} + void Initialize(v8::Handle exports, v8::Handle unused, v8::Handle context, void* priv) { mate::Dictionary dict(context->GetIsolate(), exports); dict.SetMethod("createArchive", &Archive::Create); + dict.SetMethod("initAsarSupport", &InitAsarSupport); } } // namespace diff --git a/atom/common/lib/asar.coffee b/atom/common/lib/asar.coffee index 713b617988..38ac2fc3b7 100644 --- a/atom/common/lib/asar.coffee +++ b/atom/common/lib/asar.coffee @@ -1,4 +1,4 @@ -asar = process.atomBinding 'asar' +asar = process.binding 'atom_common_asar' child_process = require 'child_process' path = require 'path' util = require 'util' diff --git a/atom/common/lib/asar_init.coffee b/atom/common/lib/asar_init.coffee new file mode 100644 index 0000000000..49efc902d9 --- /dev/null +++ b/atom/common/lib/asar_init.coffee @@ -0,0 +1,22 @@ +return (process, require, asarSource) -> + {createArchive} = process.binding 'atom_common_asar' + + # Make asar.coffee accessible via "require". + process.binding('natives').ATOM_SHELL_ASAR = asarSource + + # Monkey-patch the fs module. + require('ATOM_SHELL_ASAR').wrapFsWithAsar require('fs') + + # Make graceful-fs work with asar. + source = process.binding 'natives' + source.originalFs = source.fs + source.fs = """ + var src = '(function (exports, require, module, __filename, __dirname) { ' + + process.binding('natives').originalFs + + ' });'; + var vm = require('vm'); + var fn = vm.runInThisContext(src, { filename: 'fs.js' }); + fn(exports, require, module); + var asar = require('ATOM_SHELL_ASAR'); + asar.wrapFsWithAsar(exports); + """ diff --git a/atom/common/lib/init.coffee b/atom/common/lib/init.coffee index 7a60ed7296..1410f78334 100644 --- a/atom/common/lib/init.coffee +++ b/atom/common/lib/init.coffee @@ -35,21 +35,3 @@ global.clearImmediate = timers.clearImmediate if process.type is 'browser' global.setTimeout = wrapWithActivateUvLoop timers.setTimeout global.setInterval = wrapWithActivateUvLoop timers.setInterval - -# Add support for asar packages. -asar = require './asar' -asar.wrapFsWithAsar fs - -# Make graceful-fs work with asar. -source = process.binding 'natives' -source.originalFs = source.fs -source.fs = """ - var src = '(function (exports, require, module, __filename, __dirname) { ' + - process.binding('natives').originalFs + - ' });'; - var vm = require('vm'); - var fn = vm.runInThisContext(src, { filename: 'fs.js' }); - fn(exports, require, module); - var asar = require(#{JSON.stringify(__dirname)} + '/asar'); - asar.wrapFsWithAsar(exports); -""" diff --git a/vendor/node b/vendor/node index 70498428ce..9cd0fad904 160000 --- a/vendor/node +++ b/vendor/node @@ -1 +1 @@ -Subproject commit 70498428ced2b8ae4ce020051f06d104b5c6c4de +Subproject commit 9cd0fad904c3ebf0af0aeec2097cad3e039a7c6d From f214e887843e0982adccf125a771a95b80126a51 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 4 Feb 2015 10:52:09 -0800 Subject: [PATCH 5/7] spec: Script forked with child_process supports asar archives --- spec/asar-spec.coffee | 8 ++++++++ spec/fixtures/module/asar.js | 4 ++++ 2 files changed, 12 insertions(+) create mode 100644 spec/fixtures/module/asar.js diff --git a/spec/asar-spec.coffee b/spec/asar-spec.coffee index ae0526f9fe..ab96d7ee3d 100644 --- a/spec/asar-spec.coffee +++ b/spec/asar-spec.coffee @@ -347,6 +347,14 @@ describe 'asar package', -> throws = -> child_process.fork p assert.throws throws, /ENOENT/ + it 'supports asar in the forked js', (done) -> + file = path.join fixtures, 'asar', 'a.asar', 'file1' + child = child_process.fork path.join(fixtures, 'module', 'asar.js') + child.on 'message', (content) -> + assert.equal content, fs.readFileSync(file).toString() + done() + child.send file + describe 'asar protocol', -> url = require 'url' remote = require 'remote' diff --git a/spec/fixtures/module/asar.js b/spec/fixtures/module/asar.js new file mode 100644 index 0000000000..1fb8750878 --- /dev/null +++ b/spec/fixtures/module/asar.js @@ -0,0 +1,4 @@ +var fs = require('fs'); +process.on('message', function(file) { + process.send(fs.readFileSync(file).toString()); +}); From a02707f998266aabdc223386b725230a75255a12 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 4 Feb 2015 11:01:01 -0800 Subject: [PATCH 6/7] Fix cpplint warnings --- atom/common/api/atom_api_asar.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/atom/common/api/atom_api_asar.cc b/atom/common/api/atom_api_asar.cc index 5d35052b71..08a39d24db 100644 --- a/atom/common/api/atom_api_asar.cc +++ b/atom/common/api/atom_api_asar.cc @@ -4,7 +4,7 @@ #include -#include "atom_natives.h" +#include "atom_natives.h" // NOLINT: This file is generated with coffee2c. #include "atom/common/asar/archive.h" #include "atom/common/native_mate_converters/file_path_converter.h" #include "native_mate/arguments.h" From 7861eec4268aef13e6a91b34f0ab35904d21981d Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 4 Feb 2015 11:18:25 -0800 Subject: [PATCH 7/7] linux: Fix building --- atom/common/api/atom_api_asar.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/atom/common/api/atom_api_asar.cc b/atom/common/api/atom_api_asar.cc index 08a39d24db..11a208ac25 100644 --- a/atom/common/api/atom_api_asar.cc +++ b/atom/common/api/atom_api_asar.cc @@ -2,6 +2,8 @@ // Use of this source code is governed by the MIT license that can be // found in the LICENSE file. +#include + #include #include "atom_natives.h" // NOLINT: This file is generated with coffee2c.