ChaiScript/unittests/emscripten_eval_test.cpp
leftibot 88d3124272 Address review: fix all compiler warnings across GCC, Clang, and MSVC
Requested by @lefticus in PR #687 review.

Fix warnings-as-errors failures on all three compiler families:

- GCC Release: replace strncpy with memcpy in mystrdup (stringop-truncation)
- GCC ASAN+UBSAN Release: add bounds check in Operator() before indexing
  m_operators array — the recursive call could exceed the array size of 12,
  which is an actual out-of-bounds bug, not just a warning
- GCC: downgrade maybe-uninitialized to warning (false positive in libstdc++
  std::regex under sanitizers + optimizations)
- AppleClang: add default cases to all switch statements on Common_Types in
  boxed_number.hpp (Wswitch-default)
- AppleClang: mark Dispatch_Engine move ctor/assignment as deleted since
  shared_mutex is non-movable (Wdefaulted-function-deleted)
- AppleClang on macOS: suppress Wpoison-system-directories for /usr/local/include
- MSVC Release: suppress C4702 (unreachable code) around if-constexpr in
  proxy_functions_detail.hpp
- Fix unused variable warnings in emscripten_eval_test.cpp when asserts are
  disabled in Release mode

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-14 12:06:25 -06:00

50 lines
1.2 KiB
C++

// Test that validates the eval patterns used by the Emscripten wrapper.
// Based on work by Rob Loach (https://github.com/RobLoach/ChaiScript.js)
#ifndef CHAISCRIPT_NO_THREADS
#define CHAISCRIPT_NO_THREADS
#endif
#ifndef CHAISCRIPT_NO_DYNLOAD
#define CHAISCRIPT_NO_DYNLOAD
#endif
#include <chaiscript/chaiscript.hpp>
#include "../emscripten/chaiscript_eval.hpp"
#include <cassert>
#include <cmath>
#include <string>
int main() {
chaiscript_eval("var x = 42");
const std::string s = chaiscript_eval_string("to_string(x)");
assert(s == "42");
static_cast<void>(s);
const int i = chaiscript_eval_int("1 + 2");
assert(i == 3);
static_cast<void>(i);
bool b = chaiscript_eval_bool("true");
assert(b == true);
b = chaiscript_eval_bool("false");
assert(b == false);
static_cast<void>(b);
const float f = chaiscript_eval_float("1.5f");
assert(std::abs(f - 1.5f) < 0.001f);
static_cast<void>(f);
const double d = chaiscript_eval_double("3.14");
assert(std::abs(d - 3.14) < 0.001);
static_cast<void>(d);
chaiscript_eval("def square(n) { return n * n; }");
const int sq = chaiscript_eval_int("square(7)");
assert(sq == 49);
static_cast<void>(sq);
return 0;
}