Permalink
Browse files

Make `require` work

  • Loading branch information...
1 parent 6bebdda commit c8701ee55f7ae832b7897d58774053c9242ec17a @execjosh committed Apr 24, 2013
Showing with 129 additions and 57 deletions.
  1. +5 −1 src/bootstrap.js
  2. +18 −15 src/main.cpp
  3. +21 −13 src/modules/child_process.js
  4. +35 −26 src/modules/fs.js
  5. +41 −1 src/modules/system.js
  6. +9 −1 src/modules/webpage.js
View
@@ -76,10 +76,12 @@ phantom.__defineErrorSignalHandler__ = function(obj, page, handlers) {
});
};
+/*
(function() {
var handlers = {};
phantom.__defineErrorSignalHandler__(phantom, phantom.page, handlers);
})();
+*/
// TODO: Make this output to STDERR
phantom.defaultErrorHandler = function(message, stack) {
@@ -104,6 +106,8 @@ phantom.callback = function(callback) {
return ret;
};
+window = this;
+
(function() {
// CommonJS module implementation follows
@@ -153,7 +157,7 @@ phantom.callback = function(callback) {
}
function basename(path) {
- return path.replace(/.*\//, '');
+ return "string" === path ? path.replace(/.*\//, '') : "";
}
function joinPath() {
View
@@ -31,15 +31,29 @@
#include <QApplication>
#include <QIcon>
+#include <QString>
+#include "config.h"
#include "js/pjsengine.h"
+#include "utils.h"
+
+#include <iostream>
int main(int argc, char** argv, const char** envp)
{
Q_UNUSED(envp)
QApplication app(argc, argv);
+ QStringList args = QApplication::arguments();
+ Config conf;
+ conf.init(&args);
+
+ if (conf.scriptFile().isEmpty()) {
+ std::cerr << "USAGE: phantomjs <script>" << std::endl;
+ return -1;
+ }
+
app.setWindowIcon(QIcon(":/phantomjs-icon.png"));
app.setApplicationName("PhantomJS");
app.setOrganizationName("Ofi Labs");
@@ -52,21 +66,10 @@ int main(int argc, char** argv, const char** envp)
return -1;
}
- pjse.evaluate(
- "var timeoutId = setTimeout(function () {"
- "console.log('Hello,', timeoutId, '!');"
- "phantom.exit(0);"
- "}, 3000);"
- "var omfg = 5;"
- "var intervalId = setInterval(function () {"
- "if (--omfg) {"
- "console.log(intervalId, omfg);"
- "} else {"
- "clearInterval(intervalId);"
- "console.log(intervalId, omfg, '-- clearInterval');"
- "}"
- "}, 300);"
- );
+ pjse.evaluate(Utils::readResourceFileUtf8(":/bootstrap.js"), ":/bootstrap.js");
+
+ QString source = Utils::readResourceFileUtf8(conf.scriptFile());
+ pjse.evaluate(source, conf.scriptFile());
if (pjse.isTerminated()) {
// TODO: return value...
@@ -4,7 +4,7 @@
/*
This file is part of the PhantomJS project from Ofi Labs.
- Copyright (C) 2012 execjosh, http://execjosh.blogspot.com
+ Copyright (C) 2012-13 execjosh, http://execjosh.blogspot.com
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
@@ -30,12 +30,20 @@
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+function defineProperty(o, name, value) {
+ Object.defineProperty(o, name, {value: value});
+}
+
+function exportProperty(name, value) {
+ defineProperty(exports, name, value);
+}
+
var NOP = function () {}
/**
* spawn(command, [args], [options])
*/
-exports.spawn = function (cmd, args, opts) {
+exportProperty("spawn", function spawn(cmd, args, opts) {
var ctx = newContext()
if (null == opts) {
@@ -48,23 +56,23 @@ exports.spawn = function (cmd, args, opts) {
ctx._start(cmd, args)
return ctx
-}
+})
/**
* exec(command, [options], callback)
*/
-exports.exec = function (cmd, opts, cb) {
+exportProperty("exec", function exec(cmd, opts, cb) {
if (null == cb) {
cb = NOP
}
return cb(new Error("NotYetImplemented"))
-}
+})
/**
* execFile(file, args, options, callback)
*/
-exports.execFile = function (cmd, args, opts, cb) {
+exportProperty("execFile", function execFile(cmd, args, opts, cb) {
var ctx = newContext()
if (null == cb) {
@@ -95,14 +103,14 @@ exports.execFile = function (cmd, args, opts, cb) {
ctx._start(cmd, args)
return ctx
-}
+})
/**
* fork(modulePath, [args], [options])
*/
-exports.fork = function (modulePath, args, opts) {
+exportProperty("fork", function fork(modulePath, args, opts) {
throw new Error("NotYetImplemented")
-}
+})
// private
@@ -112,7 +120,7 @@ function newContext() {
// TODO: "Buffer" the signals and redispatch them?
- ctx.on = function (evt, cb) {
+ defineProperty(ctx, "on", function (evt, cb) {
var handler
switch (evt) {
@@ -127,10 +135,10 @@ function newContext() {
if (isFunction(handler)) {
handler.connect(cb)
}
- }
+ })
- ctx.stdout = new FakeReadableStream("stdout")
- ctx.stderr = new FakeReadableStream("stderr")
+ defineProperty(ctx, "stdout", new FakeReadableStream("stdout"))
+ defineProperty(ctx, "stderr", new FakeReadableStream("stderr"))
// Emulates `Readable Stream`
function FakeReadableStream(streamName) {
View
@@ -5,6 +5,7 @@
This file is part of the PhantomJS project from Ofi Labs.
Copyright (C) 2011 Ivan De Marino <ivan.de.marino@gmail.com>
+ Copyright (C) 2012-13 execjosh, http://execjosh.blogspot.com
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
@@ -30,6 +31,14 @@
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+function defineProperty(o, name, value) {
+ Object.defineProperty(o, name, {value: value});
+}
+
+function exportProperty(name, value) {
+ defineProperty(exports, name, value);
+}
+
// JavaScript "shim" to throw exceptions in case a critical operation fails.
/** Convert a modeOrOpts to a map
@@ -69,14 +78,14 @@ function modeOrOptsToOpts(modeOrOpts) {
* - charset An IANA, case insensitive, charset name.
* @return "file" object
*/
-exports.open = function (path, modeOrOpts) {
+exportProperty("open", function open(path, modeOrOpts) {
// Open file
var file = exports._open(path, modeOrOptsToOpts(modeOrOpts));
if (file) {
return file;
}
throw "Unable to open file '" + path + "'";
-};
+});
/** Open, read and return text content of a file.
* It will throw an exception if it fails.
@@ -89,7 +98,7 @@ exports.open = function (path, modeOrOpts) {
* - charset An IANA, case insensitive, charset name.
* @return file content
*/
-exports.read = function (path, modeOrOpts) {
+exportProperty("read", function read(path, modeOrOpts) {
if (typeof modeOrOpts == 'string') {
if (modeOrOpts.toLowerCase() == 'b') {
// open binary
@@ -111,7 +120,7 @@ exports.read = function (path, modeOrOpts) {
f.close();
return content;
-};
+});
/** Open and write text content to a file
* It will throw an exception if it fails.
@@ -124,7 +133,7 @@ exports.read = function (path, modeOrOpts) {
* - mode (see Open Mode above)
* - charset An IANA, case insensitive, charset name.
*/
-exports.write = function (path, content, modeOrOpts) {
+exportProperty("write", function write(path, content, modeOrOpts) {
var opts = modeOrOptsToOpts(modeOrOpts);
// ensure we open for writing
if ( typeof opts.mode !== 'string' ) {
@@ -136,97 +145,97 @@ exports.write = function (path, content, modeOrOpts) {
f.write(content);
f.close();
-};
+});
/** Return the size of a file, in bytes.
* It will throw an exception if it fails.
*
* @param path Path of the file to read the size of
* @return File size in bytes
*/
-exports.size = function (path) {
+exportProperty("size", function size(path) {
var size = exports._size(path);
if (size !== -1) {
return size;
}
throw "Unable to read file '" + path + "' size";
-};
+});
/** Copy a file.
* It will throw an exception if it fails.
*
* @param source Path of the source file
* @param destination Path of the destination file
*/
-exports.copy = function (source, destination) {
+exportProperty("copy", function copy(source, destination) {
if (!exports._copy(source, destination)) {
throw "Unable to copy file '" + source + "' at '" + destination + "'";
}
-};
+});
/** Copy a directory tree.
* It will throw an exception if it fails.
*
* @param source Path of the source directory tree
* @param destination Path of the destination directory tree
*/
-exports.copyTree = function (source, destination) {
+exportProperty("copyTree", function copyTree(source, destination) {
if (!exports._copyTree(source, destination)) {
throw "Unable to copy directory tree '" + source + "' at '" + destination + "'";
}
-};
+});
/** Move a file.
* It will throw an exception if it fails.
*
* @param source Path of the source file
* @param destination Path of the destination file
*/
-exports.move = function (source, destination) {
+exportProperty("move", function move(source, destination) {
exports.copy(source, destination);
exports.remove(source);
-};
+});
/** Removes a file.
* It will throw an exception if it fails.
*
* @param path Path of the file to remove
*/
-exports.remove = function (path) {
+exportProperty("remove", function remove(path) {
if (!exports._remove(path)) {
throw "Unable to remove file '" + path + "'";
}
-};
+});
/** Removes a directory.
* It will throw an exception if it fails.
*
* @param path Path of the directory to remove
*/
-exports.removeDirectory = function (path) {
+exportProperty("removeDirectory", function removeDirectory(path) {
if (!exports._removeDirectory(path)) {
throw "Unable to remove directory '" + path + "'";
}
-};
+});
/** Removes a directory tree.
* It will throw an exception if it fails.
*
* @param path Path of the directory tree to remove
*/
-exports.removeTree = function (path) {
+exportProperty("removeTree", function removeTree(path) {
if (!exports._removeTree(path)) {
throw "Unable to remove directory tree '" + path + "'";
}
-};
+});
-exports.touch = function (path) {
+exportProperty("touch", function touch(path) {
exports.write(path, "", 'a');
-};
+});
// Path stuff
-exports.join = function() {
+exportProperty("join", function join() {
var args = [];
if (0 < arguments.length) {
@@ -251,9 +260,9 @@ exports.join = function() {
var ret = args.join("/");
return 0 < ret.length ? ret : ".";
-};
+});
-exports.split = function (path) {
+exportProperty("split", function split(path) {
if (typeof path !== "string") {
return [];
}
@@ -265,4 +274,4 @@ exports.split = function (path) {
.replace(/\/$/, "")
// And split
.split("/")
-};
+});
Oops, something went wrong.

0 comments on commit c8701ee

Please sign in to comment.