Skip to content

Commit

Permalink
add support for native assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
petkaantonov committed Mar 15, 2014
1 parent 3c6a517 commit 0178b84
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ module.exports = function( grunt ) {
process.stderr
];
var flags = node11 ? ["--harmony-generators"] : [];
flags.push("--allow-natives-syntax");
if( file.indexOf( "mocha/") > -1 || file === "aplus.js" ) {
var node = spawn(typeof node11 === "string" ? node11 : 'node',
flags.concat(["../mocharun.js", file]),
Expand Down Expand Up @@ -594,7 +595,6 @@ module.exports = function( grunt ) {
var fs = require("fs");
var path = require("path");
var done = this.async();
var adapter = global.adapter = require(BUILD_DEBUG_DEST);

var totalTests = 0;
var testsDone = 0;
Expand Down
16 changes: 8 additions & 8 deletions ast_passes.js
Original file line number Diff line number Diff line change
Expand Up @@ -491,9 +491,14 @@ var astPasses = module.exports = {
if( callee.type === "Identifier" &&
callee.name === "ASSERT" ) {
if( node.arguments.length !== 1 ) {
throw new Error( "Invalid amount of arguments to ASSERT" +
src.substring(start, end)
);
results.push({
start: start,
end: end,
toString: function() {
return src.substring(start, end);
}
});
return;
}

var expr = node.arguments[0];
Expand Down Expand Up @@ -523,11 +528,6 @@ var astPasses = module.exports = {

if( callee.type === "Identifier" &&
callee.name === "ASSERT" ) {
if( node.arguments.length !== 1 ) {
throw new Error( "Invalid amount of arguments to ASSERT" +
src.substring(start, end)
);
}
var e = end + 1;
var s = start - 1;

Expand Down
33 changes: 33 additions & 0 deletions src/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,42 @@ module.exports = (function(){
return AssertionError;
})();

function getParams(args) {
var params = [];
for (var i = 0; i < args.length; ++i) params.push("arg" + i);
return params;
}

function nativeAssert(callName, args, expect) {
try {
var params = getParams(args);
var constructorArgs = params;
constructorArgs.push("return " +
callName + "("+ params.join(",") + ");");
var fn = Function.apply(null, constructorArgs);
return fn.apply(null, args);
}
catch (e) {
if (!(e instanceof SyntaxError)) {
throw e;
}
else {
return expect;
}
}
}

return function assert(boolExpr, message) {
if (boolExpr === true) return;

if (typeof boolExpr === "string" &&
boolExpr.charAt(0) === "%") {
var nativeCallName = boolExpr;
INLINE_SLICE(args, arguments, 2);
if (nativeAssert(nativeCallName, args, message) === message) return;
message = (nativeCallName + " !== " + message);
}

var ret = new AssertionError(message);
if (Error.captureStackTrace) {
Error.captureStackTrace(ret, assert);
Expand Down
3 changes: 3 additions & 0 deletions src/promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -1207,4 +1207,7 @@ Promise.CancellationError = CancellationError;
Promise.TimeoutError = TimeoutError;
Promise.TypeError = TypeError;
Promise.RejectionError = RejectionError;

util.toFastProperties(Promise);
util.toFastProperties(Promise.prototype);
};
6 changes: 1 addition & 5 deletions src/promisify.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ var makeNodePromisified = canEvaluate
? makeNodePromisifiedEval
: makeNodePromisifiedClosure;

function f(){}
function _promisify(callback, receiver, isAll) {
if (isAll) {
var methods = inheritedMethods(callback);
Expand All @@ -235,10 +234,7 @@ function _promisify(callback, receiver, isAll) {
makeNodePromisified(originalKey, THIS,
key, fn);
}
//Right now the above loop will easily turn the
//object into hash table in V8
//but this will turn it back. Yes I am ashamed.
if (methods.length > 16) f.prototype = callback;
util.toFastProperties(callback);
return callback;
}
else {
Expand Down
12 changes: 11 additions & 1 deletion src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,15 @@ function thrower(r) {
}


function toFastProperties(obj) {
/*jshint -W027*/
function f() {}
f.prototype = obj;
ASSERT("%HasFastProperties", true, obj);
return f;
eval(obj);
}

var ret = {
thrower: thrower,
isArray: es5.isArray,
Expand All @@ -170,7 +179,8 @@ var ret = {
withAppended: withAppended,
asString: asString,
maybeWrapAsError: maybeWrapAsError,
wrapsPrimitiveReceiver: wrapsPrimitiveReceiver
wrapsPrimitiveReceiver: wrapsPrimitiveReceiver,
toFastProperties: toFastProperties
};

module.exports = ret;

0 comments on commit 0178b84

Please sign in to comment.