Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

117 lines (105 sloc) 3.331 kb
<html><body>
Script Uri: <input id='script_uri' type='text' value='' style='width:400;'/>
<a id='load' href='#'>Load Script</a>
<br/>
Object to extern: <input id='object_to_extern' type='text' value='' style='width:400;'/>
<a id='go' href='#'>Go!</a>
<br/>
<textarea id='console' rows='1000' cols='100'></textarea>
<br/>
<script src='/spec_helpers/jquery.js'></script>
</body></html>
<script>
var externed = '';
var done = {};
$('#load').click(loadScript);
$('#go').click(go);
function loadScript() {
var uri = $('#script_uri').val();
if (!uri) {
alert('Please enter script to load');
return false;
}
console.log('Loading: ' + uri);
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = uri;
document.body.appendChild(script);
console.log('Successfully loaded the script at: ' + uri);
return false;
};
function go() {
var name = $('#object_to_extern').val();
if (!name) {
alert('Please enter an object to extern. Example: "PhoneGap"');
return false;
}
var o = eval(name);
if (!o) {
alert('Could not find the specified object.');
return false;
}
done = {};
externed = '/** @constructor */\nfunction ' + name + '() {};';
externObject(o, name + '.prototype.');
$('#console').val(externed);
};
function externObject(o, prefix) {
for (var i in o) {
if (!isValidPropName(i)) continue; // Invalid prop name
var prop = o[i];
var type = typeof(prop);
var name = prefix + i;
var key = name + '_' + type;
if (done[key] === 1) { continue; }
done[key] = 1;
if (type === 'function') {
externFunction(name, prop);
} else if (type === 'object') {
externInnerObject(name, prop, i)
} else if (type === 'boolean') {
externed += '\n/** @type {boolean} */\n' + name + ';';
} else if (type === 'number') {
externed += '\n/** @type {number} */\n' + name + ';';
} else if (type === 'number') {
externed += '\n/** @type {' + type + '} */\n' + name + ';';
}
}
};
function isValidPropName(propName) {
var re = /^[a-zA-Z]+[a-zA-Z_0-9]*$/;
var valid = re.test(propName);
return valid;
};
function externFunction(name, prop) {
var desc = prop.toString();
var paramNames = getParamNames(desc);
var ret = hasReturn(desc);
if (ret || paramNames.length) {
externed += '\n/**';
for (var i = 0; i < paramNames.length; i++) {
externed += '\n * @param {*} ' + paramNames[i];
}
if (ret) { externed += '\n * @return {*}'; }
externed += '\n */';
}
externed += '\n' + name +
' = function(' + paramNames.join(',') + ') {};';
};
function getParamNames(desc) {
desc = desc.substring(desc.indexOf('(') + 1);
desc = desc.substring(0, desc.indexOf(')'));
return !desc ? [] : desc.split(',');
};
function hasReturn(desc) {
return desc.indexOf('return ') >= 0;
};
function externInnerObject(name, prop, fieldName) {
var key = fieldName + '_function';
if (done[key] === 1) { return; }
done[key] = 1;
externed += '\n/** @constructor */\nfunction ' + fieldName + '() {};';
externed += '\n/** @type {' + fieldName +'} */\n' + name + ';';
externObject(prop, fieldName + '.prototype.');
};
</script>
Jump to Line
Something went wrong with that request. Please try again.