Skip to content

Error while outputting #j "xx" #613

Open

Description

@vlad-km
CL-USER> (setf nn #j"aa")
#\SIMPLE-ERROR: ref vector out of range
    at https://jscl-project.github.io/jscl.js:43039:7
    at https://jscl-project.github.io/jscl.js:43042:3
    at Object.JSCL_USER_ERROR [as fvalue] (https://jscl-project.github.io/jscl.js:43043:3)
    at internals.error (https://jscl-project.github.io/jscl.js:83:12)
    at https://jscl-project.github.io/jscl.js:9583:26
    at https://jscl-project.github.io/jscl.js:9585:3
    at JSCL_USER_AREF (https://jscl-project.github.io/jscl.js:9586:3)
    at Object.mvcall (https://jscl-project.github.io/jscl.js:73:10)
    at https://jscl-project.github.io/jscl.js:6670:27
    at https://jscl-project.github.io/jscl.js:6671:3
    at Object.JSCL_USER_CHARNAME [as fvalue] (https://jscl-project.github.io/jscl.js:6672:3)
    at https://jscl-project.github.io/jscl.js:26285:205
    at JSCL_USER_WRITECHARAUX (https://jscl-project.github.io/jscl.js:26286:3)
    at Object.mvcall (https://jscl-project.github.io/jscl.js:73:10)
    at https://jscl-project.github.io/jscl.js:26404:54
    at https://jscl-project.github.io/jscl.js:26443:3
    at Object.JSCL_USER_WRITEAUX [as fvalue] (https://jscl-project.github.io/jscl.js:26447:4)
    at JSCL_USER_NIL (https://jscl-project.github.io/jscl.js:26561:7)
    at Object.mvcall (https://jscl-project.github.io/jscl.js:73:10)
    at https://jscl-project.github.io/jscl.js:26570:18
... 23 more frames, increase JSCL::*BACKTRACE-LIMIT* to view more
CL-USER> (setf nn #j"aaa")
#j"aaa"
```lisp

Activity

  1. kparen commented on Apr 9, 2026

    @kparen
    Member

    It's probably confused between whether #j"aa" is a CHARACTER or STRING...

  2. vlad-km commented on Apr 9, 2026

    @vlad-km
    MemberAuthor

    It's probably confused between whether #j"aa" is a CHARACTER or STRING...

    I think so.
    A two-character string is a Unicode character. Maybe internals.char_to_codepoint.

    CL-USER> (setf nn (jscl::jsstring "aaa"))
    
    #j"aaa"
    CL-USER> (setf nn (jscl::jsstring "a"))
    #\a
    CL-USER> (setf nn (jscl::jsstring "aa"))
    #\SIMPLE-ERROR: ref vector out of range```
  3. iwami4438 commented on Aug 18, 2026

    @iwami4438
    Contributor

    Hello.
    Upon investigation, it appears the issue stemmed from an attempt to support UTF-16, where a sequence with a length of 2 was being treated as a single character.

    ;; In src/compiler/compiler.lisp, 1381
    (define-builtin characterp (x)
      `(selfcall
        (var (x ,x))
        (return ,(convert-to-bool
                  `(and (== (typeof x) "string")
                        (or (== (get x "length") 1)
                            (== (get x "length") 2)))))))

    However, simply removing that part does not seem to solve the problem. Please take a look at this MDN article.
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Segmenter

    I also think this article will be helpful for us. (It is written in Japanese.)
    https://qiita.com/bon127/items/491b25e90208188dafbd

    As a solution, I propose using the library mentioned in the article above.
    https://www.npmjs.com/package/graphemesplit

    It does add a dependency, which is a bit of a hassle, though.
    Please consider this suggestion.

  4. iwami4438 commented on Aug 18, 2026

    @iwami4438
    Contributor

    The investigation was prompted by aref.

    Details
    CL-USER> (trace aref)
    
    (AREF)
    
    CL-USER> #j"aa"
    
    #\0: (AREF #("NULL" "START_OF_HEADING" "START_OF_TEXT" "END_OF_TEXT" 
    "END_OF_TRANSMISSION" "ENQUIRY" "ACKNOWLEDGE" "BELL" "Backspace" "Tab" "Newline" 
    "LINE_TABULATION" "Page" "Return" "SHIFT_OUT" "SHIFT_IN" "DATA_LINK_ESCAPE" 
    "DEVICE_CONTROL_ONE" "DEVICE_CONTROL_TWO" "DEVICE_CONTROL_THREE" 
    "DEVICE_CONTROL_FOUR" "NEGATIVE_ACKNOWLEDGE" "SYNCHRONOUS_IDLE" 
    "END_OF_TRANSMISSION_BLOCK" "CANCEL" "END_OF_MEDIUM" "SUBSTITUTE" "ESCAPE" 
    "INFORMATION_SEPARATOR_FOUR" "INFORMATION_SEPARATOR_THREE" 
    "INFORMATION_SEPARATOR_TWO" "INFORMATION_SEPARATOR_ONE" "Space" 
    "EXCLAMATION_MARK" "QUOTATION_MARK" "NUMBER_SIGN" "DOLLAR_SIGN" 
    "PERCENT_SIGN" "AMPERSAND" "APOSTROPHE" "LEFT_PARENTHESIS" "RIGHT_PARENTHESIS" 
    "ASTERISK" "PLUS_SIGN" "COMMA" "HYPHEN-MINUS" "FULL_STOP" "SOLIDUS" "DIGIT_ZERO" 
    "DIGIT_ONE" "DIGIT_TWO" "DIGIT_THREE" "DIGIT_FOUR" "DIGIT_FIVE" "DIGIT_SIX" 
    "DIGIT_SEVEN" "DIGIT_EIGHT" "DIGIT_NINE" "COLON" "SEMICOLON" "LESS-THAN_SIGN" 
    "EQUALS_SIGN" "GREATER-THAN_SIGN" "QUESTION_MARK" "COMMERCIAL_AT" 
    "LATIN_CAPITAL_LETTER_A" "LATIN_CAPITAL_LETTER_B" "LATIN_CAPITAL_LETTER_C" 
    "LATIN_CAPITAL_LETTER_D" "LATIN_CAPITAL_LETTER_E" "LATIN_CAPITAL_LETTER_F" 
    "LATIN_CAPITAL_LETTER_G" "LATIN_CAPITAL_LETTER_H" "LATIN_CAPITAL_LETTER_I" 
    "LATIN_CAPITAL_LETTER_J" "LATIN_CAPITAL_LETTER_K" "LATIN_CAPITAL_LETTER_L" 
    "LATIN_CAPITAL_LETTER_M" "LATIN_CAPITAL_LETTER_N" "LATIN_CAPITAL_LETTER_O" 
    "LATIN_CAPITAL_LETTER_P" "LATIN_CAPITAL_LETTER_Q" "LATIN_CAPITAL_LETTER_R" 
    "LATIN_CAPITAL_LETTER_S" "LATIN_CAPITAL_LETTER_T" "LATIN_CAPITAL_LETTER_U" 
    "LATIN_CAPITAL_LETTER_V" "LATIN_CAPITAL_LETTER_W" "LATIN_CAPITAL_LETTER_X" 
    "LATIN_CAPITAL_LETTER_Y" "LATIN_CAPITAL_LETTER_Z" "LEFT_SQUARE_BRACKET" 
    "REVERSE_SOLIDUS" "RIGHT_SQUARE_BRACKET" "CIRCUMFLEX_ACCENT" "LOW_LINE" 
    "GRAVE_ACCENT" "LATIN_SMALL_LETTER_A" "LATIN_SMALL_LETTER_B" 
    "LATIN_SMALL_LETTER_C" "LATIN_SMALL_LETTER_D" "LATIN_SMALL_LETTER_E" 
    "LATIN_SMALL_LETTER_F" "LATIN_SMALL_LETTER_G" "LATIN_SMALL_LETTER_H" 
    "LATIN_SMALL_LETTER_I" "LATIN_SMALL_LETTER_J" "LATIN_SMALL_LETTER_K" 
    "LATIN_SMALL_LETTER_L" "LATIN_SMALL_LETTER_M" "LATIN_SMALL_LETTER_N" 
    "LATIN_SMALL_LETTER_O" "LATIN_SMALL_LETTER_P" "LATIN_SMALL_LETTER_Q" 
    "LATIN_SMALL_LETTER_R" "LATIN_SMALL_LETTER_S" "LATIN_SMALL_LETTER_T" 
    "LATIN_SMALL_LETTER_U" "LATIN_SMALL_LETTER_V" "LATIN_SMALL_LETTER_W" 
    "LATIN_SMALL_LETTER_X" "LATIN_SMALL_LETTER_Y" "LATIN_SMALL_LETTER_Z" 
    "LEFT_CURLY_BRACKET" "VERTICAL_LINE" "RIGHT_CURLY_BRACKET" "TILDE" "Rubout") 
    -56514463) ;; <this
    
    ...error...
  5. vlad-km commented on Aug 18, 2026

    @vlad-km
    MemberAuthor

    Upon investigation, it appears the issue stemmed from an attempt to support UTF-16, where a sequence with a length of 2 was being treated as a single character.

    That’s not the bottom of the rabbit hole yet.

    In prelude.js, there is a function:

    internals.char_from_codepoint = function (x)
    

    which incorrectly identifies those very graphemes. Consequently, the compiler misidentifies the object.
    Furthermore, in char.lisp, the char-name function stumbles over a missing slot in the +ascii-name+ array.

    The author of char.lisp hasn't maintained their creation for a long time,
    and I currently have no way to set up SBCL for debugging.

    As for integrating third-party libraries for graphemes - I don't think that's the best idea.

    V.

  6. iwami4438 commented on Aug 18, 2026

    @iwami4438
    Contributor

    Hmm, I see. Alternatively, by using codePointAt in this way, it was possible to treat two characters that do not form a surrogate pair (e.g., "aa") as a single string.

    However, this does not mean they are treated as a "single character." 👨‍👩‍👧‍👦 is still determined to be a string composed of multiple code points.

    (define-builtin characterp (x)
      `(selfcall
        (var (x ,x))
        (return ,(convert-to-bool
                  `(and (== (typeof x) "string")
                        (or (== (get x "length") 1)
                            (and (== (get x "length") 2)
                                 (>= (method-call x "codePointAt" 0) #x10000)))))))) ;; check surrogate pair
  7. hemml commented on Aug 18, 2026

    @hemml
    Contributor

    It can be fixed the following way:

    diff --git a/src/compiler/compiler.lisp b/src/compiler/compiler.lisp
    index 045bc62..3ec0ff0 100644
    --- a/src/compiler/compiler.lisp
    +++ b/src/compiler/compiler.lisp
    @@ -1384,7 +1384,11 @@
         (return ,(convert-to-bool
                   `(and (== (typeof x) "string")
                         (or (== (get x "length") 1)
    -                        (== (get x "length") 2)))))))
    +                        (and (== (get x "length") 2)
    +                             (>= (method-call x "charCodeAt" 0) 55296)
    +                             (<= (method-call x "charCodeAt" 0) 56319))))))))
    

    Also, it will be good to check the character code to be <128:

    diff --git a/src/char.lisp b/src/char.lisp
    index e7967ff..bda1372 100644
    --- a/src/char.lisp
    +++ b/src/char.lisp
    @@ -281,7 +281,7 @@ For the first 32 characters ('C0 controls'), the first
       ;; not much in FORMAT yet.  I'm only implementing ASCII names right
       ;; now, since Unicode is kind of big.
       (let ((code (char-code char)))
    -    (if (<= code 127)
    +    (if (and (> code 0) (< code 128))
             (aref +ascii-names+ code)
           nil)))  ;; for now, no name
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions