diff --git a/.gitignore b/.gitignore index 28b0a90f..671b4b7a 100644 --- a/.gitignore +++ b/.gitignore @@ -9,9 +9,6 @@ node_modules build/* npm-debug.log -src/colony/modules/events.js -src/colony/modules/domain.js - out gyp diff --git a/Makefile b/Makefile index 44fa8808..0ce1d119 100644 --- a/Makefile +++ b/Makefile @@ -16,8 +16,6 @@ else ninja -C out/$(CONFIG) endif -NODE_FILES = deps/node-libs/events.js deps/node-libs/domain.js - .PHONY: all test test-colony test-node all: colony @@ -31,7 +29,6 @@ nuke: update: git submodule update --init --recursive - cp $(NODE_FILES) src/colony/modules/ npm install test: test-node test-colony diff --git a/libcolony.gyp b/libcolony.gyp index 092a7d3f..52de7e54 100644 --- a/libcolony.gyp +++ b/libcolony.gyp @@ -107,7 +107,8 @@ 'src/colony/modules/crypto.js', 'src/colony/modules/dgram.js', 'src/colony/modules/dns.js', - 'src/colony/modules/events.js', + 'deps/node-libs/domain.js', + 'deps/node-libs/events.js', 'src/colony/modules/fs.js', 'src/colony/modules/http.js', 'src/colony/modules/https.js', diff --git a/src/colony/lua/cli.lua b/src/colony/lua/cli.lua index 0051f36c..889d77fa 100644 --- a/src/colony/lua/cli.lua +++ b/src/colony/lua/cli.lua @@ -16,14 +16,14 @@ local colony = require('colony') -- This is temporary until we can add files to builtin array easily. if _tessel_lib then - colony.precache['tessel'] = _tessel_lib - colony.run('tessel') + colony.precache['tessel'] = _tessel_lib + colony.run('tessel') end -- also temporary. if _wifi_cc3000_lib then - colony.precache['wifi-cc3000'] = _wifi_cc3000_lib - colony.run('wifi-cc3000') + colony.precache['wifi-cc3000'] = _wifi_cc3000_lib + colony.run('wifi-cc3000') end -- Command line invocation @@ -37,5 +37,17 @@ if string.sub(p, 1, 1) ~= '.' then end colony.global:setImmediate(function () - colony.run(p) + -- TODO move uncaughtException handling to an appropriate file + local status, err = pcall(function () + colony.run(p) + end) + if not status then + if colony.global.process.domain then + colony.global.process.domain:emit('error', err) + elseif colony.global.process:listeners('uncaughtException').length then + colony.global.process:emit('uncaughtException', err) + else + error(err) + end + end end) diff --git a/src/colony/lua/colony-js.lua b/src/colony/lua/colony-js.lua index eb0b7eaf..bb5d44af 100644 --- a/src/colony/lua/colony-js.lua +++ b/src/colony/lua/colony-js.lua @@ -605,6 +605,43 @@ arr_proto.indexOf = function (this, searchElement, fromIndex) return -1 end +arr_proto.lastIndexOf = function (this, searchElement, fromIndex) + local len = this.length + local start + + if len == 0 then + return -1 + end + + if fromIndex ~= nil then + fromIndex = tonumber(fromIndex) + else + fromIndex = len + end + + if fromIndex >= 0 then + start = fromIndex + else + start = len + fromIndex + + if start < 0 then + start = 0 + end + end + + if start >= len then + start = len - 1 + end + + for i=start,0,-1 do + if this[i] == searchElement then + return i + end + end + + return -1 +end + arr_proto.map = function (this, fn, ...) local a = js_arr({}, 0) local args = table.pack(...) diff --git a/src/colony/lua/preload.lua b/src/colony/lua/preload.lua index 12ee0518..b2f51004 100644 --- a/src/colony/lua/preload.lua +++ b/src/colony/lua/preload.lua @@ -118,6 +118,10 @@ do global.process.cwd = function () return tm.cwd() end + global.process._usingDomains = function () + -- process._usingDomains called by domain.js in Node + -- here it's a no-op + end global.process.hrtime = function (this, prev) -- This number exceeds the 53-bit limit on integer representation, but with -- microsecond resolution, there are only ~50 bits of actual data diff --git a/test/suite/array.js b/test/suite/array.js index 058b38e1..d6482cc7 100644 --- a/test/suite/array.js +++ b/test/suite/array.js @@ -1,6 +1,6 @@ var tap = require('../tap'); -tap.count(50); +tap.count(56); function arreq (a, b) { if (a.length != b.length) { @@ -178,3 +178,12 @@ arr.sort(function (a, b) { }); tap.eq(arr.join(','), '-11,-6,2,10'); + +// lastIndexOf +var array = [2, 5, 9, 2]; +tap.eq(array.lastIndexOf(2), 3, 'lastIndexOf') +tap.eq(array.lastIndexOf(7), -1, 'lastIndexOf') +tap.eq(array.lastIndexOf(2, 3), 3, 'lastIndexOf') +tap.eq(array.lastIndexOf(2, 2), 0, 'lastIndexOf') +tap.eq(array.lastIndexOf(2, -2), 0, 'lastIndexOf') +tap.eq(array.lastIndexOf(2, -1), 3, 'lastIndexOf') diff --git a/test/suite/domain.js b/test/suite/domain.js new file mode 100644 index 00000000..1c1d590a --- /dev/null +++ b/test/suite/domain.js @@ -0,0 +1,29 @@ +var tap = require('../tap') +var domain = require('domain'); +var EventEmitter = require('events').EventEmitter; + +tap.count(2); + +test1(); + +function test1 () { + var d = domain.create(); + d.on('error', function(er) { + tap.ok(true, 'thrown error caught') + test2(); + }); + d.run(function() { + throw new Error('lol') + }); +} + +function test2 () { + var d = domain.create(); + d.on('error', function(er) { + tap.ok(true, 'emitted error caught') + }); + d.run(function() { + var e = new EventEmitter(); + e.emit('error', new Error('lol')) + }); +}