Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Issue #3 : stringify alternative before better stringify available (avoid crashes on setter #5

Merged
merged 1 commit into from

2 participants

@frantzmiccoli
Collaborator

I'm working on a patch to json.js and json2.js. This should fix the bug on node codein.

@frantzmiccoli frantzmiccoli REFS ketamynx#3 this fixes getter's issues, another commit will be do…
…ne to json2 to have a more robust of stringify and then JSON2 should be used instead of that dirty patch
6f3dbe4
@frantzmiccoli frantzmiccoli commented on the diff
stringify.js
((27 lines not shown))
+ return typeof c === 'string'
+ ? c
+ : '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
+ }) + '"' : '"' + string + '"';
+ }
+
+ function str(key, holder) {
+ var i,
+ k,
+ v,
+ length,
+ mind = gap,
+ partial,
+ value;
+
+ //
@frantzmiccoli Collaborator

Interesting part starts here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@ketamynx ketamynx merged commit 7e9bf09 into ketamynx:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 6, 2012
  1. @frantzmiccoli

    REFS ketamynx#3 this fixes getter's issues, another commit will be do…

    frantzmiccoli authored
    …ne to json2 to have a more robust of stringify and then JSON2 should be used instead of that dirty patch
This page is out of date. Refresh to see the latest.
Showing with 163 additions and 3 deletions.
  1. +3 −2 debugger.js
  2. +1 −1  package.json
  3. +159 −0 stringify.js
View
5 debugger.js
@@ -3,8 +3,9 @@ var is_file = function(f){ try{ return require('fs').lstatSync(f).isFile(); }cat
var file_get_contents = function(f,mode){return (!file_exists(f))? '' : require('fs').readFileSync(f, mode); };
var get_constr = function(v){ return(v===null)?"[object Null]":Object.prototype.toString.call(v); };
// var fnprefix = (["FUNCTION"].concat(process.hrtime()).concat(process.hrtime())).join('.'); // ONLY WORKS IN LATER Vs
-var fnprefix = 'TYPE_FUNC_'+(new Date().getTime());
-var jsencr = function(o){ var e = []; return JSON.stringify(o, function(k,v){
+var fnprefix = 'TYPE_FUNC_'+(new Date().getTime());
+var stringify = require('./stringify.js');
+var jsencr = function(o){ var e = []; return stringify(o, function(k,v){
if(typeof(v)==='function') return fnprefix+v.toString();
if(typeof(v)!=='object' || v===null) return v;
for(var i in e){ if(e[i]===v){ return "Circular"; }};
View
2  package.json
@@ -3,7 +3,7 @@
"name": "node-codein",
"description": "NodeJS Console Object Debug Inspector",
"keywords": ["console","inspect","inspector","object","tree","outline","viewer","debug","debugger","webkit","runtime","eval"],
- "version": "1.0.2",
+ "version": "1.0.3",
"homepage": "https://github.com/ketamynx/node-codein",
"repository": {
"type": "git",
View
159 stringify.js
@@ -0,0 +1,159 @@
+//
+// Public domain
+// This whole code come from Douglas Crockford's JSON2 library except some
+// minor change on the first line of the str() function (see next comment)
+//
+//
+(function() {
+ var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
+ escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
+ gap,
+ indent,
+ meta = {
+ '\b': '\\b',
+ '\t': '\\t',
+ '\n': '\\n',
+ '\f': '\\f',
+ '\r': '\\r',
+ '"' : '\\"',
+ '\\': '\\\\'
+ },
+ rep;
+
+ function quote(string) {
+ escapable.lastIndex = 0;
+ return escapable.test(string) ? '"' + string.replace(escapable, function (a) {
+ var c = meta[a];
+ return typeof c === 'string'
+ ? c
+ : '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
+ }) + '"' : '"' + string + '"';
+ }
+
+ function str(key, holder) {
+ var i,
+ k,
+ v,
+ length,
+ mind = gap,
+ partial,
+ value;
+
+ //
@frantzmiccoli Collaborator

Interesting part starts here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ // Changes from Douglas Crockford's version, the five next code lines
+ // avoid errors when accessing a value through a getter that raise an
+ // ulgy error
+ // previously : value = holder[key];
+ //
+
+ try {
+ value = holder[key];
+ } catch (Error) {
+ value = "accessor error";
+ }
+
+ if (value && typeof value === 'object' &&
+ typeof value.toJSON === 'function') {
+ value = value.toJSON(key);
+ }
+
+ if (typeof rep === 'function') {
+ value = rep.call(holder, key, value);
+ }
+
+ switch (typeof value) {
+ case 'string':
+ return quote(value);
+
+ case 'number':
+ return isFinite(value) ? String(value) : 'null';
+
+ case 'boolean':
+ case 'null':
+ return String(value);
+
+
+ case 'object':
+ if (!value) {
+ return 'null';
+ }
+
+
+ gap += indent;
+ partial = [];
+
+
+ if (Object.prototype.toString.apply(value) === '[object Array]') {
+
+
+ length = value.length;
+ for (i = 0; i < length; i += 1) {
+ partial[i] = str(i, value) || 'null';
+ }
+
+ v = partial.length === 0
+ ? '[]'
+ : gap
+ ? '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']'
+ : '[' + partial.join(',') + ']';
+ gap = mind;
+ return v;
+ }
+
+ if (rep && typeof rep === 'object') {
+ length = rep.length;
+ for (i = 0; i < length; i += 1) {
+ if (typeof rep[i] === 'string') {
+ k = rep[i];
+ v = str(k, value);
+ if (v) {
+ partial.push(quote(k) + (gap ? ': ' : ':') + v);
+ }
+ }
+ }
+ } else {
+ for (k in value) {
+ if (Object.prototype.hasOwnProperty.call(value, k)) {
+ v = str(k, value);
+ if (v) {
+ partial.push(quote(k) + (gap ? ': ' : ':') + v);
+ }
+ }
+ }
+ }
+
+ v = partial.length === 0
+ ? '{}'
+ : gap
+ ? '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}'
+ : '{' + partial.join(',') + '}';
+ gap = mind;
+ return v;
+ }
+ };
+
+ stringify = function stringify(value, replacer, space) {
+ var i;
+ gap = '';
+ indent = '';
+
+ if (typeof space === 'number') {
+ for (i = 0; i < space; i += 1) {
+ indent += ' ';
+ }
+ } else if (typeof space === 'string') {
+ indent = space;
+ }
+
+ rep = replacer;
+ if (replacer && typeof replacer !== 'function' &&
+ (typeof replacer !== 'object' ||
+ typeof replacer.length !== 'number')) {
+ throw new Error('JSON.stringify');
+ }
+
+ return str('', {'': value});
+ };
+
+ module.exports = stringify;
+})();
Something went wrong with that request. Please try again.