Permalink
Browse files

Automatically loads require.js and include.js and a few others (see

builtins/).

Converted many of the lib/ (include) files to modules (require).

This is an in-progress, but working commit.  There may be some stray
debug prints to the console.
  • Loading branch information...
1 parent f7427b6 commit 1bbc7199ceeb120732fb9d17478e38117c5db957 @mschwartz committed Mar 15, 2012
View
@@ -3,9 +3,6 @@
* and open the template in the editor.
*/
-include('lib/forEach.js');
-include('lib/require.js');
-
process = require('builtin/process');
var examples = [
@@ -37,13 +34,15 @@ function main() {
exec('cp -r examples /usr/share/silkjs');
println('-- copying httpd to /usr/share/silkjs/');
exec('cp -r httpd /usr/share/silkjs');
+ println('-- copying SilkJS CORE JavaScript library files to /usr/share/silkjs/');
+ exec('cp -r builtin /usr/share/silkjs');
println('-- copying SilkJS JavaScript library files to /usr/share/silkjs/');
exec('cp -r lib /usr/share/silkjs');
println('-- copying SilkJS JavaScript module files to /usr/share/silkjs/');
exec('cp -r modules /usr/share/silkjs');
println('-- copying examples to /usr/local/bin');
- forEach(examples, function(example) {
+ examples.each(function(example) {
exec('cp examples/'+example+' /usr/local/bin');
exec('chmod 755 /usr/local/bin/'+example);
});
@@ -2,7 +2,7 @@ Error.stackTraceLimit = 50;
Error.prepareStackTrace = function(error, structuredStackTrace) {
var stack = [];
- forEach(structuredStackTrace, function(item) {
+ forstructuredStackTrace.each(function(item) {
stack.push({
scriptName: item.getScriptNameOrSourceURL(),
typeName: item.getTypeName(),
@@ -92,7 +92,7 @@ Error.exceptionHandler = function(e) {
if (ex.stack) {
// ex.stack.pop();
var stack = '';
- forEach(ex.stack, function(item) {
+ ex.stack.each(function(item) {
if (res) {
stack += '<li>';
}
View
@@ -0,0 +1,8 @@
+//log('adding builtins');
+
+include('builtin/print_r.js');
+include('builtin/extensions.js');
+include('builtin/Exceptions.js');
+include('builtin/include.js');
+include('builtin/require.js');
+include('builtin/xhr.js');
View
@@ -0,0 +1,67 @@
+/*
+ * @module extensions
+ *
+ * Provides handy extensions to builtin JS types
+ */
+
+Array.prototype.each = Object.prototype.each = Function.prototype.each = function(fun) {
+ for (var key in this) {
+ if (this.hasOwnProperty && this.hasOwnProperty(key)) {
+ if (fun.call(this, this[key], key, this) === false) {
+ return;
+ }
+ }
+ }
+};
+
+String.prototype.endsWith = function(suffix) {
+ return this.indexOf(suffix, this.length - suffix.length) !== -1;
+};
+
+String.prototype.trim = function() {
+ return this.replace(/^\s+|\s+$/, '');
+};
+
+String.prototype.endsWith = function endsWith(c){
+ if(this.charAt(this.length - 1) == c){
+ return true;
+ }
+ else {
+ return false;
+ }
+};
+
+String.prototype.startsWith = function startsWith(c){
+ if(this.charAt(0) == c){
+ return true;
+ }
+ else {
+ return false;
+ }
+};
+
+String.prototype.replaceAll = function replaceAll(a, b){
+ var s = this;
+ while(s.indexOf(a) > -1){
+ s = s.replace(a, b);
+ }
+ return s;
+};
+
+Object.prototype.extend = function() {
+ var me = this;
+ arguments.each(function(o) {
+ for (var key in o) {
+ me[key] = o[key];
+ }
+ });
+};
+
+Array.prototype.format = Object.prototype.format = function() {
+ return builtin.print_r(this);
+}
+
+Array.prototype.dump = Object.prototype.dump = function(out) {
+ out = out || println;
+ out(this.format());
+};
@@ -4,14 +4,14 @@
function locateFile(fn) {
if (fs.isFile(fn)) {
- return fn;
- if (fs.isFile(fn + '.js')) {
- return fn + '.js';
- }
- else if (fs.isFile(fn + '.coffee')) {
- return fn + '.coffee';
- }
- }
+ return fn;
+ }
+ if (fs.isFile(fn + '.js')) {
+ return fn + '.js';
+ }
+ else if (fs.isFile(fn + '.coffee')) {
+ return fn + '.coffee';
+ }
if (fn.substr(0,1) == '/' || fn.substr(0,2) == './' || fn.substr(0,3) == '../') {
throw 'Could not locate include file ' + fn;
}
@@ -32,7 +32,7 @@
return fn + '.coffee';
}
}
- throw 'Could not locate include file ' + fn;
+ throw Error('Could not locate include file ' + fn);
}
var suffix = '.coffee',
suffixLen = suffix.length;
@@ -45,7 +45,7 @@
// var lines = contents.split('\n');
// var lineNum = 0;
// var source = '';
-// forEach(lines, function(line) {
+// lines.each(function(line) {
// source += (++lineNum) + ': ' + line + '\n';
// });
//
View
@@ -0,0 +1,76 @@
+/**
+ * Concatenates the values of a variable into an easily readable string
+ * by Matt Hackett [scriptnode.com]
+ * @param {Object} x The variable to debug
+ * @param {Number} max The maximum number of recursions allowed (keep low, around 5 for HTML elements to prevent errors) [default: 10]
+ * @param {String} sep The separator to use between [default: a single space ' ']
+ * @param {Number} l The current level deep (amount of recursion). Do not use this parameter: it's for the function's own use
+ */
+builtin.print_r = function(x, max, sep, l) {
+
+ l = l || 0;
+ max = max || 10;
+ sep = sep || ' ';
+
+ if (l > max) {
+ return "[WARNING: Too much recursion]\n";
+ }
+
+ var
+ i,
+ r = '',
+ t = typeof x,
+ tab = '';
+
+ if (x === null) {
+ r += "(null)\n";
+ } else if (t == 'object') {
+
+ l++;
+
+ for (i = 0; i < l; i++) {
+ tab += sep;
+ }
+
+ if (x && x.length) {
+ t = 'array';
+ }
+
+ r += '(' + t + ") :\n";
+
+ for (i in x) {
+ if (typeof x[i] == 'function' && i in Object.prototype) {
+ continue;
+ }
+ try {
+ r += tab + '[' + i + '] : ' + builtin.print_r(x[i], max, sep, (l + 1));
+ } catch(e) {
+ return "[ERROR: " + e + "]\n";
+ }
+ }
+
+ } else {
+ if (t == 'string') {
+ if (x == '') {
+ x = '(empty)';
+ }
+ }
+
+ if (t == 'function') {
+ if (x.toString().length > 64) {
+ r += 'function() { ... }';
+ }
+ else {
+ r += x.toString();
+ }
+ }
+ else {
+ r += '(' + t + ') ';
+ r += x;
+ }
+ r += "\n";
+ }
+
+ return r;
+
+}
@@ -15,11 +15,11 @@
function locateFile(module) {
function tryFile(path) {
var tryPath = fs.realpath(path);
- if (fs.isFile(tryPath)) {
+ if (tryPath && fs.isFile(tryPath)) {
return tryPath;
}
tryPath = fs.realpath(require.fsPath + path);
- if (fs.isFile(tryPath)) {
+ if (tryPath && fs.isFile(tryPath)) {
return tryPath;
}
return false;
@@ -73,7 +73,7 @@
},
getAllResponseHeaders: function() {
var headers = [];
- forEach(responseHeaders, function(value, key) {
+ responseHeaders.each(function(value, key) {
headers.push(key + ': ' + value);
});
return headers.join('\r\n');
@@ -3,7 +3,6 @@
/*
* Sample program to test ssh exec
*/
-include('lib/require.js');
include('lib/ssh.js');
function main(hostname, username, password) {
View
@@ -4,10 +4,7 @@
* and open the template in the editor.
*/
-include('lib/require.js');
-include('lib/forEach.js');
include('lib/phpjs.js');
-include('lib/print_r.js');
var imports = [
'move',
@@ -35,7 +32,7 @@ fs = require('builtin/fs');
process = require('builtin/process');
v8 = require('builtin/v8');
-forEach(imports, function(value) {
+imports.each(function(value) {
global[value] = ncurses[value];
});
@@ -69,7 +66,7 @@ function PrintLn(row, col, prompt, value) {
function GetStat() {
var ret = {};
var lines = fs.readFile('/proc/stat').split('\n');
- forEach(lines, function(line) {
+ lines.each(function(line) {
var vals = line.split(/\s+/);
var key = vals.shift();
ret[key] = vals.join(' ');
@@ -82,7 +79,7 @@ function InitState() {
current = {};
initial = {};
stat = GetStat();
- forEach(stat, function(value, key) {
+ stat.each(function(value, key) {
current[key] = value.split(/\s+/).join(':::');
initial[key] = value.split(/\s+/).join(':::');
});
@@ -132,7 +129,7 @@ function PrintCPU() {
move(row++, 0);
printw(" user sys nice idle user sys nice idle ");
attroff(A_BOLD); attroff(A_STANDOUT);
- forEach(stat, function(value, key) {
+ stat.each(function(value, key) {
if (key.indexOf('cpu') == 0) {
var prompt = key.toUpperCase();
var parts = stat[key].split(/\s+/);
@@ -198,7 +195,7 @@ function PrintMemory() {
}
var memInfo = {};
var lines = fs.readFile('/proc/meminfo').split('\n');
- forEach(lines, function(line) {
+ lines.each(function(line) {
line = line.replace(/\s+/g, '');
var parts = line.split(':');
memInfo[parts[0]] = parseInt(parts[1], 10);
@@ -235,7 +232,7 @@ function PrintDisk() {
attroff(A_BOLD); attroff(A_STANDOUT);
var lines = fs.readFile('/proc/diskstats').split('\n');
- forEach(lines, function(line) {
+ lines.each(function(line) {
line = line.replace(/^\s+/g, '');
var arr = line.split(/\s+/);
var drive = arr[2];
@@ -276,7 +273,7 @@ function PrintVirtualMemory() {
var lines = fs.readFile('/proc/vmstat').split('\n');
var vmstat = {};
- forEach(lines, function(line) {
+ lines.each(function(line) {
var parts = line.split(/\s+/);
vmstat[parts[0]] = parseInt(parts[1], 10);
});
@@ -2,7 +2,6 @@
* program to test require
*/
-include('lib/require.js');
var console = require('builtin/console');
function main() {
View
@@ -2,8 +2,6 @@
* Program to test XMLHttpRequest implementation
*/
-include('lib/require.js');
-
include('lib/print_r.js');
include('lib/xhr.js');
@@ -6,8 +6,6 @@
* To change this template use File | Settings | File Templates.
*/
-include('lib/include.js');
-include('lib/require.js');
include('lib/coffee-script.js');
include('lib/print_r.js');
include('lib/forEach.js');
View
@@ -153,7 +153,7 @@ HttpChild = (function() {
res.redirect(req.uri + '/');
}
var found = '';
- forEach(Config.directoryIndex, function(index) {
+ Config.directoryIndex.each(function(index) {
var f = fn;
f += '/';
f += index;
@@ -5,7 +5,7 @@
<body>
<h1>Test Jst</h1>
<ul>
- <% forEach(SQL.getDataRows('select * from STATS_1011 LIMIT 25'), function(row) { %>
+ <% SQL.getDataRows('select * from STATS_1011 LIMIT 25').each(function(row) { %>
<li><%= row.firstName %> <%= row.lastName %> (<%= row.team %>)</li>
<% }); %>
</uL>
Oops, something went wrong.

0 comments on commit 1bbc719

Please sign in to comment.