Skip to content

Commit

Permalink
Added in strict equality checks
Browse files Browse the repository at this point in the history
  • Loading branch information
twolfson committed Sep 30, 2012
1 parent a765b44 commit eac96c9
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 27 deletions.
50 changes: 25 additions & 25 deletions lib/jsmin.c.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function jsminFn(stdin, options) {

function isAlphanum(c) {
return ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
(c >= 'A' && c <= 'Z') || c == '_' || c == '$' || c == '\\' ||
(c >= 'A' && c <= 'Z') || c === '_' || c === '$' || c === '\\' ||
c > 126);
}

Expand All @@ -84,15 +84,15 @@ function jsminFn(stdin, options) {
function get() {
var c = theLookahead;
theLookahead = EOF;
if (c == EOF) {
if (c === EOF) {
c = getc(stdin);
theY = theX;
theX = c;
}
if (c >= ' ' || c == '\n' || c == EOF) {
if (c >= ' ' || c === '\n' || c === EOF) {
return c;
}
if (c == '\r') {
if (c === '\r') {
return '\n';
}
return ' ';
Expand All @@ -114,7 +114,7 @@ function jsminFn(stdin, options) {

function next() {
var c = get();
if (c == '/') {
if (c === '/') {
switch (peek()) {
case '/':
for (;;) {
Expand All @@ -128,7 +128,7 @@ function jsminFn(stdin, options) {
for (;;) {
switch (get()) {
case '*':
if (peek() == '/') {
if (peek() === '/') {
get();
return ' ';
}
Expand Down Expand Up @@ -157,60 +157,60 @@ function jsminFn(stdin, options) {
switch (d) {
case 1:
putc(theA, stdout);
if (theA == theB && (theA == '+' || theA == '-') && theY != theA) {
if (theA === theB && (theA === '+' || theA === '-') && theY !== theA) {
putc(' ', stdout);
}
case 2:
theA = theB;
if (theA == '\'' || theA == '"' || theA == '`') {
if (theA === '\'' || theA === '"' || theA === '`') {
for (;;) {
putc(theA, stdout);
theA = get();
if (theA == theB) {
if (theA === theB) {
break;
}
if (theA == '\\') {
if (theA === '\\') {
putc(theA, stdout);
theA = get();
}
if (theA == EOF) {
if (theA === EOF) {
error("Unterminated string literal.");
}
}
}
case 3:
theB = next();
if (theB == '/' && (theA == '(' || theA == ',' || theA == '=' ||
theA == ':' || theA == '[' || theA == '!' ||
theA == '&' || theA == '|' || theA == '?' ||
theA == '{' || theA == '}' || theA == ';' ||
theA == '\n')) {
if (theB === '/' && (theA === '(' || theA === ',' || theA === '=' ||
theA === ':' || theA === '[' || theA === '!' ||
theA === '&' || theA === '|' || theA === '?' ||
theA === '{' || theA === '}' || theA === ';' ||
theA === '\n')) {
putc(theA, stdout);
putc(theB, stdout);
for (;;) {
theA = get();
if (theA == '[') {
if (theA === '[') {
for (;;) {
putc(theA, stdout);
theA = get();
if (theA == ']') {
if (theA === ']') {
break;
}
if (theA == '\\') {
if (theA === '\\') {
putc(theA, stdout);
theA = get();
}
if (theA == EOF) {
if (theA === EOF) {
error("Unterminated set in Regular Expression literal.");
}
}
} else if (theA == '/') {
} else if (theA === '/') {
break;
} else if (theA =='\\') {
} else if (theA ==='\\') {
putc(theA, stdout);
theA = get();
}
if (theA == EOF) {
if (theA === EOF) {
error("Unterminated Regular Expression literal.");
}
putc(theA, stdout);
Expand All @@ -228,14 +228,14 @@ function jsminFn(stdin, options) {
*/

function jsmin() {
if (peek() == 0xEF) {
if (peek() === 0xEF) {
get();
get();
get();
}
theA = '\n';
action(3);
while (theA != EOF) {
while (theA !== EOF) {
switch (theA) {
case ' ':
if (isAlphanum(theB)) {
Expand Down
59 changes: 59 additions & 0 deletions lib/jsmin.classic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* jsmin - Refer to LICENSE in base directory
* @param {String} input JavaScript to minifiy
* @return {String} Minified JavaScript
*/
var jsminc = require('./jsmin.c');
function jsmin(input) {
var EOF = -1,
stdin = {
'index': 0,
'end': input.length,
'read': function (len) {
// For now only handle len = 1
if (len !== 1) {
throw new Error('You can only read one character from input at a time.');
}

// If we are at the end, return EOF (-1)
var index = stdin.index;
if (index === stdin.end) {
return EOF;
}

// Read the input at our index
var char = input[index];

// Increment our index
stdin.index = index + 1;

// Return char
return char;
}
},
output = '',
options = {
'error': '',
'stdout': function (str) {
// Add the string to output
output += str;
},
'stderr': function (err) {
// Add the error to output
options.error += err;
},
'exit': function (code) {
// Throw the collective error
throw new Error(options.error);
}
};

// Run jsminc
jsminc(stdin, options);

// Return the output
return output;
}

// Expose jsmin to the world
module.exports = jsmin;
6 changes: 4 additions & 2 deletions macro/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ jsminc = jsminc.replace(/function ([^\(]+)\(([^\)]*)\)[^{]+/g, function (_, fnNa
// Replace types inside functions with vars
jsminc = jsminc.replace(/int /g, 'var ');

// Upcase equality to strict equality
jsminc = jsminc.replace(/==/g, '===');
jsminc = jsminc.replace(/!=/g, '!==');

// Upcast jsmin into a reusable module
// TODO: This step is too damn big
jsminc = jsminc.replace(/var EOF = -1;(.|\n)*/, function (jsmin) {
Expand Down Expand Up @@ -92,7 +96,5 @@ jsminc = jsminc + [
'module.exports = jsminFn;'
].join('\n');

// TODO: Strict equality

// Write out the converted file
fs.writeFileSync(__dirname + '/../lib/jsmin.c.js', jsminc, 'utf8');

0 comments on commit eac96c9

Please sign in to comment.