Aerilius / sketchup-console-plus

A better Ruby Console and IDE for integrated development in SketchUp.
MIT License
42 stars 17 forks source link

Error when transfering strings with non-UTF-8 compatible bytes #24

Closed Aerilius closed 6 years ago

Aerilius commented 6 years ago

When a string with non-UTF compatible characters (e.g. binary string) is output, a JSON::GeneratorError is raised.

Example:

s = "\xFF"; nil # First byte of an image read with Sketchup::ImageRep
> nil
s.encoding
> #<Encoding:UTF-8>
s
>  Uncaught promise rejection with reason [JSON::GeneratorError]: "partial character in source, but hit end"
s.valid_encoding?
> false

In SketchUp's native console, the string is output without error (although characters are not necessarily displayed).

Ruby Console+ should behave at least as good as the native console.

Aerilius commented 6 years ago

Any output is transfered to the Console+ web dialog as JSON. The Ruby JSON library encodes to UTF-8. The binary string in Ruby defaulted to encoding UTF-8, for which the byte is an invalid character. The binary string encoding should have been ASCII-8BIT/BINARY, however that cannot be converted to UTF-8 (Encoding::UndefinedConversionError: "\xFF" from ASCII-8BIT to UTF-8).

An ideal fix would change Bridge to be able to transfer any byte strings to the web dialog.

Aerilius commented 6 years ago

In output, the native console renders invalid characters like \xFF (byte 255) with fallback glyphs ￿ (strangely \uFFFF). But in input, it actually modifies the inserted string and replaces such characters in input by ? (byte 63).

An appropriate solution is probably to suppress errors by replacing invalid characters for display of console output. It's much easier to implement in console.rb input/result handling rather than traversing objects before JSON conversion.