HTMLDialog BUG

Hi SketchUp team,
I have a problem. I found a bug in UI::HTMLDialog
It returns me an incorrect value in ruby.
Sketchup version 2021.1.332(Windows)
Sketchup version 2021.1.331(MacOS)

Exampe:

dlg = UI::HtmlDialog.new({
                           dialog_title: "test"
                         })
dlg.add_action_callback('say'){|context, params|
  p  params # => {"a" => -2074800563}
}
dlg.set_html(%Q{
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
  sketchup.say({a:2220166733})
</script>
</body>
</html>
             })
dlg.show

It is not really a “bug” instead this behaviour comes because of differences of Ruby hashes vs. JavaScript object…
(Someone with better knowledge than mine, may explain better :wink: )

Better to use JSON to communicate between…e.g. :

require 'json'
dlg = UI::HtmlDialog.new({
                           dialog_title: "test"
                         })
dlg.add_action_callback('say'){|context, params|
  p  JSON.parse(params)
}
dlg.set_html(%Q{
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
  sketchup.say(JSON.stringify({a:2220166733}));
</script>
</body>
</html>
             })
dlg.show
1 Like

Thank you for answering my question.
I know what you said about JSON serialization, but that’s not the problem.
is it correct that the number 2220166733 becomes -2074800563 ?

This is an int overflow. The max value of int on system where the type is 32bit is 2147483647.
2220166733 is larger, but somewhere being reinterpreted as int which will make it represent -2074800563.

Example can be seen in this Compiler Explorer example: Compiler Explorer

#include <iostream>
#include <limits>

int main() {
    std::cout << std::numeric_limits<int>::max() << "\n";
    std::cout << std::numeric_limits<int>::min() << "\n";

    size_t num = 2220166733;
    int overflow = static_cast<int>(num);
    std::cout << overflow << "\n";
    return 0;
}
Program returned: 0
Program stdout
2147483647
-2147483648
-2074800563

I’m not sure if this conversion happens in Chromium or in SU’s code. Would have to investigate.

Logged a bug report: `int` overflow when converting from JS number to Ruby · Issue #753 · SketchUp/api-issue-tracker · GitHub

2 Likes

OK Thank you

I guess a workaround would be to pass it as a string and convert it to an Integer in Ruby.

dlg = UI::HtmlDialog.new({
                           dialog_title: "test"
                         })
dlg.add_action_callback('say'){|context, params|
  p params['a'].to_i
  nil # Important to avoid crash
}
dlg.set_html(%Q{
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
  sketchup.say({a:'2220166733'})
</script>
</body>
</html>
             })
dlg.show
2 Likes

Thank you, your suggestion is very important to me

nil # Important to avoid crash

Yea that turned out to be another bug, when passing a large Integer from Ruby to JS. In that case we got a crash. Logged that as: Returning a large value from HtmlDialog action callback crashes SU · Issue #754 · SketchUp/api-issue-tracker · GitHub