MCPcopy Create free account
hub / github.com/dynjs/dynjs / toString

Method toString

src/main/java/org/dynjs/runtime/Types.java:403–474  ·  view source on GitHub ↗
(ExecutionContext context, Object o)

Source from the content-addressed store, hash-verified

401
402 private static Pattern EXP_PATTERN = Pattern.compile("^(.*)e([+-])([0-9]+)$");
403 public static String toString(ExecutionContext context, Object o) {
404 if (o == Types.UNDEFINED) {
405 return "undefined";
406 }
407
408 if (o == Types.NULL || o == null) {
409 return "null";
410 }
411
412 if (o instanceof JSObject) {
413 return (String) toString(context, toPrimitive(context, o, "String"));
414 }
415 if (o instanceof Number) {
416 if (((Number) o).doubleValue() == -0) {
417 return "0";
418 }
419
420 if (o instanceof Double) {
421 Double dbl = (Double) o;
422 if (dbl.doubleValue() == (long) dbl.doubleValue()) {
423 o = dbl.longValue();
424 }
425 }
426 String result = Types.rewritePossiblyExponentialValue(o.toString());
427
428 Matcher matcher = EXP_PATTERN.matcher(result);
429 if (matcher.matches()) {
430 int expValue = Integer.parseInt(matcher.group(3));
431 if (matcher.group(2).equals("+")) {
432 if (expValue <= 20) {
433 String digits = matcher.group(1).replace(".", "");
434 StringBuilder newResult = new StringBuilder();
435 int offset = 0;
436 if (digits.startsWith("-")) {
437 newResult.append("-");
438 offset = 1;
439 }
440 for (int i = offset; i <= expValue; ++i) {
441 if (i >= digits.length()) {
442 newResult.append("0");
443 } else {
444 newResult.append(digits.charAt(i));
445 }
446 }
447 if (newResult.length() < digits.length()) {
448 newResult.append(".");
449 newResult.append(digits.substring(newResult.length() - 1));
450 }
451 result = newResult.toString();
452 }
453 } else {
454 if (expValue <= 6) {
455 StringBuilder newResult = new StringBuilder().append("0.");
456 for (int i = 0; i < (expValue - 1); ++i) {
457 newResult.append(0);
458 }
459
460 String digits = matcher.group(1).replace(".", "");

Callers 15

addMethod · 0.95
interpretMethod · 0.95
interpretMethod · 0.95
interpretMethod · 0.95
interpretMethod · 0.95
callMethod · 0.95
callMethod · 0.95
callMethod · 0.95
callMethod · 0.95
callMethod · 0.95
callMethod · 0.95
callMethod · 0.95

Calls 5

toPrimitiveMethod · 0.95
groupMethod · 0.80
appendMethod · 0.80
lengthMethod · 0.80

Tested by 5

testOutputStreamMethod · 0.36
joinArrayMethod · 0.36
sayHelloMethod · 0.36