use of com.github.anba.es6draft.runtime.internal.StrBuilder in project es6draft by anba.
the class JSONObject method SerializeJSONObject.
/**
* 24.3.2.3 Runtime Semantics: SerializeJSONObject ( value )
*
* @param cx
* the execution context
* @param serializer
* the serializer state
* @param value
* the script object
*/
private static void SerializeJSONObject(ExecutionContext cx, JSONSerializer serializer, ScriptObject value) {
/* steps 1-2 */
if (!serializer.stack.add(value)) {
throw newTypeError(cx, Messages.Key.JSONCyclicValue);
}
/* steps 3-4 (not applicable) */
/* steps 5-6 */
Iterable<String> k;
if (serializer.propertyList != null) {
k = serializer.propertyList;
} else {
k = EnumerableOwnNames(cx, value);
}
/* step 7 (not applicable) */
/* steps 8-10 */
boolean isEmpty = true;
String gap = serializer.gap;
StrBuilder result = serializer.result;
result.append('{');
serializer.level += 1;
for (String p : k) {
// Inlined: SerializeJSONProperty
Object v = Get(cx, value, (Object) p);
v = TransformJSONValue(cx, serializer, value, p, v);
if (!IsJSONSerializable(v)) {
continue;
}
if (!isEmpty) {
result.append(',');
}
isEmpty = false;
if (!gap.isEmpty()) {
indent(serializer, result);
}
QuoteJSONString(result, p);
result.append(':');
if (!gap.isEmpty()) {
result.append(' ');
}
SerializeJSONValue(cx, serializer, v);
}
serializer.level -= 1;
if (!isEmpty && !gap.isEmpty()) {
indent(serializer, result);
}
result.append('}');
/* step 11 */
serializer.stack.remove(value);
/* steps 12-13 (not applicable) */
}
use of com.github.anba.es6draft.runtime.internal.StrBuilder in project es6draft by anba.
the class JSONObject method stringify.
public static String stringify(ExecutionContext cx, Object value) {
OrdinaryObject wrapper = ObjectCreate(cx, Intrinsics.ObjectPrototype);
CreateDataProperty(cx, wrapper, "", value);
StrBuilder result = new StrBuilder(cx);
JSONSerializer serializer = new JSONSerializer(null, null, "", result);
value = TransformJSONValue(cx, serializer, wrapper, "", value);
if (!IsJSONSerializable(value)) {
return "";
}
SerializeJSONValue(cx, serializer, value);
return result.toString();
}
use of com.github.anba.es6draft.runtime.internal.StrBuilder in project es6draft by anba.
the class RegExpConstructor method EscapeRegExpPattern.
/**
* 21.2.3.2 Abstract Operations for the RegExp Constructor<br>
* 21.2.3.2.4 Runtime Semantics: EscapeRegExpPattern ( P, F )
*
* @param cx
* the execution context
* @param p
* the regular expression pattern
* @param f
* the regular expression flags
* @return the escaped regular expression pattern
*/
public static String EscapeRegExpPattern(ExecutionContext cx, String p, String f) {
if (p.isEmpty()) {
return "(?:)";
}
boolean inClass = false;
StrBuilder sb = new StrBuilder(cx, p.length());
for (int i = 0, len = p.length(); i < len; ++i) {
char c = p.charAt(i);
if (c == '/' && !inClass) {
sb.append("\\/");
} else if (c == '[') {
inClass = true;
sb.append(c);
} else if (c == ']' && inClass) {
inClass = false;
sb.append(c);
} else if (c == '\\') {
assert i + 1 < len;
switch(c = p.charAt(++i)) {
case 0x0A:
sb.append("\\n");
break;
case 0x0D:
sb.append("\\r");
break;
case 0x2028:
sb.append("\\u2028");
break;
case 0x2029:
sb.append("\\u2029");
break;
default:
sb.append('\\').append(c);
break;
}
} else if (Characters.isLineTerminator(c)) {
switch(c) {
case 0x0A:
sb.append("\\n");
break;
case 0x0D:
sb.append("\\r");
break;
case 0x2028:
sb.append("\\u2028");
break;
case 0x2029:
sb.append("\\u2029");
break;
default:
throw new AssertionError();
}
} else {
sb.append(c);
}
}
return sb.toString();
}
use of com.github.anba.es6draft.runtime.internal.StrBuilder in project es6draft by anba.
the class FunctionConstructor method CreateDynamicFunction.
/**
* 19.2.1.1.1 RuntimeSemantics: CreateDynamicFunction(constructor, newTarget, kind, args)
*
* @param callerContext
* the caller execution context
* @param cx
* the execution context
* @param newTarget
* the newTarget constructor function
* @param kind
* the function kind
* @param args
* the function arguments
* @return the new function object
*/
public static FunctionObject CreateDynamicFunction(ExecutionContext callerContext, ExecutionContext cx, Constructor newTarget, SourceKind kind, Object... args) {
/* steps 1-6 (not applicable) */
/* steps 7-9 */
ScriptLoader scriptLoader = cx.getRealm().getScriptLoader();
FunctionCompiler compiler;
Intrinsics fallbackProto;
switch(kind) {
case AsyncFunction:
compiler = scriptLoader::asyncFunction;
fallbackProto = Intrinsics.AsyncFunctionPrototype;
break;
case AsyncGenerator:
compiler = scriptLoader::asyncGenerator;
fallbackProto = Intrinsics.AsyncGenerator;
break;
case Function:
compiler = scriptLoader::function;
fallbackProto = Intrinsics.FunctionPrototype;
break;
case Generator:
compiler = scriptLoader::generator;
fallbackProto = Intrinsics.Generator;
break;
default:
throw new AssertionError();
}
/* steps 10-15 */
int argCount = args.length;
String parameters, bodyText;
if (argCount == 0) {
parameters = "";
bodyText = "";
} else if (argCount == 1) {
parameters = "";
bodyText = ToFlatString(cx, args[0]);
} else {
StrBuilder sb = new StrBuilder(cx);
Object firstArg = args[0];
sb.append(ToFlatString(cx, firstArg));
int k = 2;
for (; k < argCount; ++k) {
Object nextArg = args[k - 1];
String nextArgString = ToFlatString(cx, nextArg);
sb.append(',').append(nextArgString);
}
parameters = sb.toString();
bodyText = ToFlatString(cx, args[k - 1]);
}
/* steps 16-17, 19-28 */
Source source = functionSource(kind, callerContext);
CompiledFunction compiledFunction;
try {
compiledFunction = compiler.compile(source, parameters, bodyText);
} catch (ParserException | CompilationException e) {
throw e.toScriptException(cx);
}
/* step 29 */
ScriptObject proto = GetPrototypeFromConstructor(cx, newTarget, fallbackProto);
/* steps 18, 30-38 */
return CreateDynamicFunction(cx, kind, compiledFunction, proto);
}
use of com.github.anba.es6draft.runtime.internal.StrBuilder in project es6draft by anba.
the class JSONObject method SerializeJSONArray.
/**
* 24.3.2.4 Runtime Semantics: SerializeJSONArray( value )
*
* @param cx
* the execution context
* @param serializer
* the serializer state
* @param value
* the script array object
* @param stack
* the current stack
*/
private static void SerializeJSONArray(ExecutionContext cx, JSONSerializer serializer, ScriptObject value) {
/* steps 1-2 */
if (!serializer.stack.add(value)) {
throw newTypeError(cx, Messages.Key.JSONCyclicValue);
}
/* steps 3-5 (not applicable) */
/* step 6 */
long len = ToLength(cx, Get(cx, value, "length"));
/* steps 7-10 */
String gap = serializer.gap;
StrBuilder result = serializer.result;
result.append('[');
if (len > 0) {
serializer.level += 1;
for (long index = 0; index < len; ++index) {
if (!gap.isEmpty()) {
indent(serializer, result);
}
// Inlined: SerializeJSONProperty
Object v = Get(cx, value, index);
v = TransformJSONValue(cx, serializer, value, ToString(index), v);
if (!IsJSONSerializable(v)) {
result.append("null");
} else {
SerializeJSONValue(cx, serializer, v);
}
if (index + 1 < len) {
result.append(',');
}
}
serializer.level -= 1;
if (!gap.isEmpty()) {
indent(serializer, result);
}
}
result.append(']');
/* step 11 */
serializer.stack.remove(value);
/* steps 12-13 (not applicable) */
}
Aggregations