|
|
@ -28,30 +28,89 @@ |
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
|
|
|
|
|
|
|
const kVowelSounds = {a: true, e: true, i: true, o: true, u: true, y: true}; |
|
|
|
const kCapitalVowelSounds = {a: true, e: true, i: true, o: true, u: true, |
|
|
|
h: true, f: true, l: true, m: true, n: true, r: true, s: true, x: true, |
|
|
|
y: true}; |
|
|
|
// Lazily initialized.
|
|
|
|
var kVowelSounds = 0; |
|
|
|
var kCapitalVowelSounds = 0; |
|
|
|
|
|
|
|
|
|
|
|
function GetInstanceName(cons) { |
|
|
|
if (cons.length == 0) { |
|
|
|
return ""; |
|
|
|
} |
|
|
|
var first = %StringToLowerCase(StringCharAt.call(cons, 0)); |
|
|
|
var mapping = kVowelSounds; |
|
|
|
if (kVowelSounds === 0) { |
|
|
|
kVowelSounds = {a: true, e: true, i: true, o: true, u: true, y: true}; |
|
|
|
kCapitalVowelSounds = {a: true, e: true, i: true, o: true, u: true, h: true, |
|
|
|
f: true, l: true, m: true, n: true, r: true, s: true, x: true, y: true}; |
|
|
|
} |
|
|
|
var vowel_mapping = kVowelSounds; |
|
|
|
if (cons.length > 1 && (StringCharAt.call(cons, 0) != first)) { |
|
|
|
// First char is upper case
|
|
|
|
var second = %StringToLowerCase(StringCharAt.call(cons, 1)); |
|
|
|
// Second char is upper case
|
|
|
|
if (StringCharAt.call(cons, 1) != second) |
|
|
|
mapping = kCapitalVowelSounds; |
|
|
|
if (StringCharAt.call(cons, 1) != second) { |
|
|
|
vowel_mapping = kCapitalVowelSounds; |
|
|
|
} |
|
|
|
var s = mapping[first] ? "an " : "a "; |
|
|
|
} |
|
|
|
var s = vowel_mapping[first] ? "an " : "a "; |
|
|
|
return s + cons; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const kMessages = { |
|
|
|
var kMessages = 0; |
|
|
|
|
|
|
|
|
|
|
|
function FormatString(format, args) { |
|
|
|
var result = format; |
|
|
|
for (var i = 0; i < args.length; i++) { |
|
|
|
var str; |
|
|
|
try { str = ToDetailString(args[i]); } |
|
|
|
catch (e) { str = "#<error>"; } |
|
|
|
result = ArrayJoin.call(StringSplit.call(result, "%" + i), str); |
|
|
|
} |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function ToDetailString(obj) { |
|
|
|
if (obj != null && IS_OBJECT(obj) && obj.toString === $Object.prototype.toString) { |
|
|
|
var constructor = obj.constructor; |
|
|
|
if (!constructor) return ToString(obj); |
|
|
|
var constructorName = constructor.name; |
|
|
|
if (!constructorName) return ToString(obj); |
|
|
|
return "#<" + GetInstanceName(constructorName) + ">"; |
|
|
|
} else { |
|
|
|
return ToString(obj); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function MakeGenericError(constructor, type, args) { |
|
|
|
if (IS_UNDEFINED(args)) { |
|
|
|
args = []; |
|
|
|
} |
|
|
|
var e = new constructor(kAddMessageAccessorsMarker); |
|
|
|
e.type = type; |
|
|
|
e.arguments = args; |
|
|
|
return e; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* Setup the Script function and constructor. |
|
|
|
*/ |
|
|
|
%FunctionSetInstanceClassName(Script, 'Script'); |
|
|
|
%SetProperty(Script.prototype, 'constructor', Script, DONT_ENUM); |
|
|
|
%SetCode(Script, function(x) { |
|
|
|
// Script objects can only be created by the VM.
|
|
|
|
throw new $Error("Not supported"); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// Helper functions; called from the runtime system.
|
|
|
|
function FormatMessage(message) { |
|
|
|
if (kMessages === 0) { |
|
|
|
kMessages = { |
|
|
|
// Error
|
|
|
|
cyclic_proto: "Cyclic __proto__ value", |
|
|
|
// TypeError
|
|
|
@ -109,58 +168,8 @@ const kMessages = { |
|
|
|
result_not_primitive: "Result of %0 must be a primitive, was %1", |
|
|
|
invalid_json: "String '%0' is not valid JSON", |
|
|
|
circular_structure: "Converting circular structure to JSON" |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
function FormatString(format, args) { |
|
|
|
var result = format; |
|
|
|
for (var i = 0; i < args.length; i++) { |
|
|
|
var str; |
|
|
|
try { str = ToDetailString(args[i]); } |
|
|
|
catch (e) { str = "#<error>"; } |
|
|
|
result = ArrayJoin.call(StringSplit.call(result, "%" + i), str); |
|
|
|
} |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function ToDetailString(obj) { |
|
|
|
if (obj != null && IS_OBJECT(obj) && obj.toString === $Object.prototype.toString) { |
|
|
|
var constructor = obj.constructor; |
|
|
|
if (!constructor) return ToString(obj); |
|
|
|
var constructorName = constructor.name; |
|
|
|
if (!constructorName) return ToString(obj); |
|
|
|
return "#<" + GetInstanceName(constructorName) + ">"; |
|
|
|
} else { |
|
|
|
return ToString(obj); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function MakeGenericError(constructor, type, args) { |
|
|
|
if (IS_UNDEFINED(args)) { |
|
|
|
args = []; |
|
|
|
}; |
|
|
|
} |
|
|
|
var e = new constructor(kAddMessageAccessorsMarker); |
|
|
|
e.type = type; |
|
|
|
e.arguments = args; |
|
|
|
return e; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* Setup the Script function and constructor. |
|
|
|
*/ |
|
|
|
%FunctionSetInstanceClassName(Script, 'Script'); |
|
|
|
%SetProperty(Script.prototype, 'constructor', Script, DONT_ENUM); |
|
|
|
%SetCode(Script, function(x) { |
|
|
|
// Script objects can only be created by the VM.
|
|
|
|
throw new $Error("Not supported"); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// Helper functions; called from the runtime system.
|
|
|
|
function FormatMessage(message) { |
|
|
|
var format = kMessages[message.type]; |
|
|
|
if (!format) return "<unknown message " + message.type + ">"; |
|
|
|
return FormatString(format, message.args); |
|
|
|