question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

runtime error on JS

See original GitHub issue

There is a runtime error on JS on the last switch. edit: There is a weird runtime error with the following code:

import ceylon.collection {ArrayList}

by("Zambonifofex")
shared class Pattern(String pattern)
{
    shared actual String string => "Pattern(``pattern``)";

    alias PatternPossibilities => [{Anything*}*]|<Boolean(Character)>;

    [Integer, {Anything*}]? walktrought(PatternPossibilities patternPossibilities, Integer from = 0)
    {
        switch(patternPossibilities)
        case(is Boolean(Character))
        {
            if(exists first = pattern[from], patternPossibilities(first))
            {
                return([from + 1, {first}]);
            }
        }
        else
        {
            for(value option in patternPossibilities)
            {
                variable value results = ArrayList<Anything>();
                variable value i = from;
                for(value inner in option)
                {
                    "All the elements of all the elements of patternPossibility must be of type `PatternPossibility`"
                    assert(is PatternPossibilities inner);

                    if(exists result = walktrought(inner, i))
                    {
                        i = result[0];
                        results.add(result[1]);
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    return([i, results]);
                }
            }
        }
        return(null);
    }

    Boolean regular(Character character) => !(character in {'0', 'E', '#', '.', ',', ';', '\''});

    PatternPossibilities padding() => [{}];

    PatternPossibilities char(Boolean predicate(Character character))
    {
        PatternPossibilities ne() => [{padding(), not('\''.equals), [{'\''.equals, char(predicate)}, {padding(), ne()}]}];
        return([{'\''.equals, '\''.equals, [{padding(), char(predicate)}]}, {padding(), predicate, [{padding(), char(predicate)}]}, {'\''.equals, not('\''.equals), [{padding(), ne()}]}, {}]);
    }

    PatternPossibilities minimumExponent() => [{'0'.equals, [{minimumExponent()}, {}]}];
    PatternPossibilities exponent() => [{'E'.equals, minimumExponent()}, {}];
    PatternPossibilities optionalFraction() => [{'#'.equals, [{optionalFraction()}]}, {}];
    PatternPossibilities fraction() => [{[{'0'.equals, fraction()}]}, {optionalFraction()}];
    PatternPossibilities minimumInteger() => [{'0'.equals, [{','.equals}, {}], [{minimumInteger()}]}, {}];
    PatternPossibilities integer() => [{'#'.equals, [{','.equals}, {}], [{integer()}, {minimumInteger()}]}];
    PatternPossibilities number() => [{[{integer()}, {minimumInteger()}], [{'.'.equals, fraction()}, {}], exponent()}];
    PatternPossibilities prefix() => char(or('E'.equals, regular));
    PatternPossibilities suffix() => char(regular);
    PatternPossibilities positivePattern() => [{prefix(), number(), suffix()}];
    PatternPossibilities negativePattern() => [{';'.equals, positivePattern()}, {}];
    PatternPossibilities anych() => [{padding(), function(Character c) => true, [{padding(), anych()}]}, {}];
    PatternPossibilities zero() => [{';'.equals, anych()}, {}];
    PatternPossibilities main() => [{positivePattern(), negativePattern(), zero()}];

    value result = walktrought(main()) else [-1, []];

    value [total, parsed] = result;

    "Invalid formatting pattern"
    assert(total == pattern.size);

    String getString({Anything*} stream)
    {
        if(is {Anything*} cont = stream.rest.rest.first, is {Anything*} continuation = cont.rest.first, is {Anything*} second = stream.rest.first, exists character = second.first)
        {
            return(character.string + getString(continuation));
        }
        else
        {
            return("");
        }
    }

    assert(is {Anything*} first = parsed.first, is {Anything*} ffirst = first.first, is{Anything*} sfirst = first.rest.first, is {Anything*} tfirst = first.rest.rest.first, is {Anything*} fsfirst = sfirst.first, is {Anything*} ssfirst = sfirst.rest.first, is {Anything*} sssfirst = ssfirst.rest.first else {});

    String positivePrefix = getString(ffirst);

    String positiveSuffix = getString(tfirst);

    String negativePrefix;

    String negativeSuffix;

    String? zeroString;

    if(is {Anything*} third = parsed.rest.rest.first, is {Anything*} sthird = third.rest.first)
    {
        zeroString = getString(sthird);
    }
    else
    {
        zeroString = null;
    }

    variable Integer forced = 0;

    variable Integer forcedFr = 0;

    variable Integer count = 0;

    variable Integer countFr = 1;

    void calculateFormat({Anything*} parsed)
    {
        if(is {Anything*} firstp = parsed.first, is {Anything*} ffirstp = firstp.first, is {Anything*} sfirstp = firstp.rest.first, is {Anything*} tfirstp = firstp.rest.rest.first, exists fffirstp = ffirstp.first)
        {
            if(exists fsfirstp = sfirstp.first)
            {
                count = 0;
            }
            else
            {
                count++;
            }

            if(fffirstp == '0')
            {
                forced++;
            }

            calculateFormat(tfirstp);
        }
    }

    void calculateFormatFr({Anything*} parsed)
    {
        if(is {Anything*} firstp = parsed.first, is {Anything*} ffirstp = firstp.first, is {Anything*} sfirstp = firstp.rest.first, is Character fffirst = ffirst.first)
        {
            if(fffirst == '0')
            {
                forcedFr++;
            }

            countFr++;

            calculateFormatFr(sfirstp);
        }
    }

    calculateFormat(fsfirst);
    calculateFormatFr(sssfirst);

    assert(is {Anything*} sec = parsed.rest.first);

    if(is {Anything*} second = sec.rest.first)
    {
        assert(is {Anything*} fsecond = second.first, is {Anything*} tsecond = second.rest.rest.first);

        negativePrefix = getString(fsecond);
        negativeSuffix = getString(tsecond);
    }
    else
    {
        negativePrefix = "-";
        negativeSuffix = "";
    }

    String combined = positivePrefix + positiveSuffix + negativePrefix + negativeSuffix;

    Float(Float) divide;

    if(combined.contains("%"))
    {
        divide = shuffle(Float.times)(100.0);
    }
    else
    {
        divide = identity<Float>;
    }

    String formatNum(Integer number, Integer base, Correspondence<Integer, Character|String> digits)
    {
        switch(number <=> 0)
        case(smaller)
        {
            return(formatNum(-number, base, digits));
        }
        case(equal)
        {
            return("");
        }
        case(larger)
        {
            return(((digits[number % base]?.string) else "#") + formatNum(number / base, base, digits));
        }
    }

    shared String format(variable Float number, Boolean? fraction = null, Integer base = 10, Correspondence<Integer, Character> digits = String('0'..'9') + String('A'..'Z'))
    {
        number = divide(number);

        String pre;
        String pos;

        if(number.negative)
        {
            pre => negativePrefix;
            pos => negativeSuffix;
        }
        else if(number == 0.0, exists zeroString)
        {
            return(zeroString);
        }
        else
        {
            pre => positivePrefix;
            pos => positiveSuffix;
        }

        String cut(String second, String first = "", String comma = "")
        {
            if(second.empty)
            {
                return(first);
            }
            else if(count.zero)
            {
                return(second);
            }
            else
            {
                value div = second.slice(count);
                return(cut(div[1], first + comma + div[0], ","));
            }
        }

        String int = cut(formatNum(number.integer, base, digits).padTrailing(forced, digits[0] else '0')).reversed;
        String frac;

        if(fraction else number.fractionalPart != 0.0)
        {
            frac = "." + formatNum((number.integer * (base ^ countFr)) - ((number * (base ^ countFr)) + 0.0).integer, base, digits).padTrailing(forcedFr, digits[0] else '0').reversed;
        }
        else
        {
            frac = "";
        }

        return(pre + int + frac + pos);
    }
}

shared void run()
{
    for(value pattern in {"", "+;-;zero", "POS ;NEG ;NUL 0", "$ #,##0.00", "##0,.0## %"})
    {
        for(value number in {123456.0, 123456.789999, 0.0, -123.0, 12.0, -12.345})
        {
            print(Pattern(pattern).format(number));
        }
    }
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
chochoscommented, Aug 23, 2016

This minimal example fails:

class Test(Object x) {
  assert(is {Object*} s=x, is {Character*} s2=s);
  print(s);
  print(s2);
  shared void test() {
    print(s);
    print(s2);
    if (is String s3=s, is Character c=s.first) {
      print(s3);
      print(s2);
      print(c);
    }
  }
  test();
}

shared void run() {
  value t = Test("ok");
  t.test();
}
1reaction
chochoscommented, Aug 23, 2016

@jvasileff yeah that hasn’t been solved yet for JS. But that’s a separate issue.

This particular problem is related to a special condition inside an assert in an initializer block. The variables created by assert in that case should become attributes instead of just local variables.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What happens when there is a javascript runtime error?
A run-time error can be catastrophic depends on where it happened and what does the piece of code when it occurs. In this...
Read more >
JavaScript - Errors & Exceptions Handling - Tutorialspoint
Runtime errors, also called exceptions, occur during execution (after compilation/interpretation). For example, the following line causes a runtime error ...
Read more >
Javascript | Error and Exceptional Handling With Examples
Runtime Error: A runtime error is an error that occurs during the running of the program, also known as the exceptions. In the...
Read more >
Handling runtime errors in JavaScript using try/catch/finally
An exception is an error that occurs at runtime due to an illegal operation during execution. Examples of exceptions include trying to reference...
Read more >
Error - JavaScript - MDN Web Docs - Mozilla
Error objects are thrown when runtime errors occur. The Error object can also be used as a base object for user-defined exceptions.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found