Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Big rewrite of the JS bridge stuff. Removed that silly prototype wrap…
…ping, instead I'm totally encapsulating the bindings and native objects and exposing pure JS constructors. Wrote an argument marshalling/validating lib to do strict checking on args coming into JS bridge.
- Loading branch information
Sam Day
committed
May 30, 2012
1 parent
375fdb3
commit f5b0efd
Showing
5 changed files
with
145 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
build/ | ||
node_modules/ | ||
lib/gitteh.js | ||
lib/args.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
module.exports = fn = (params) -> | ||
paramList = for own name, param of params | ||
param.name = name | ||
param | ||
ret = [] | ||
|
||
numRequired = 0 | ||
leftRequired = for param in paramList | ||
break if param.hasOwnProperty("default") | ||
numRequired++ | ||
param | ||
paramList.splice 0, leftRequired.length | ||
rightRequired = for param in paramList.slice(0).reverse() | ||
break if param.hasOwnProperty("default") | ||
numRequired++ | ||
param | ||
rightRequired = rightRequired.reverse() | ||
paramList.splice -rightRequired.length | ||
|
||
args = Array.prototype.slice.call fn.caller.arguments | ||
throw new Error("Not enough arguments.") if args.length < numRequired | ||
|
||
left = args.splice 0, leftRequired.length | ||
right = args.splice -rightRequired.length | ||
|
||
argn = 0 | ||
for arg, i in left | ||
argn++ | ||
param = leftRequired[i] | ||
if not fn.validators[param.type] arg | ||
throw new TypeError "#{param.name} (#{argn}) is not a valid #{param.type}" | ||
ret.push arg | ||
|
||
for param, i in paramList | ||
argn++ | ||
if args.length > i | ||
arg = args[i] | ||
if not fn.validators[param.type] arg | ||
throw new TypeError "#{param.name} (#{argn}) is not a valid #{param.type}" | ||
ret.push arg | ||
else ret.push param.default | ||
for arg, i in right | ||
argn++ | ||
param = rightRequired[i] | ||
if not fn.validators[param.type] arg | ||
throw new TypeError "#{param.name} (#{argn}) is not a valid #{param.type}" | ||
ret.push arg | ||
return ret | ||
fn.validators = | ||
string: (val) -> | ||
return typeof val is "string" | ||
function: (val) -> return typeof val is "function" | ||
bool: (val) -> | ||
return typeof val is "boolean" | ||
|
||
### | ||
myfn = -> | ||
[path, bare, cb] = fn | ||
path: type: "string" | ||
bare: type: "bool", default: false | ||
cb: type: "function" | ||
console.log path, bare, cb | ||
myfn "awesome", "zzz" | ||
### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters