Skip to content

Commit

Permalink
[translation] Able to throw error::Strict.
Browse files Browse the repository at this point in the history
Figured out that we need to catch it by reference...
  • Loading branch information
Andy Chu committed May 2, 2020
1 parent 65bee82 commit bdfa6ed
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 24 deletions.
3 changes: 2 additions & 1 deletion build/mycpp.sh
Expand Up @@ -659,7 +659,8 @@ cpp-unit-tests() {

local name='unit_tests'
mkdir -p _bin
compile _bin/$name cpp/unit_tests.cc cpp/frontend_match.cc mycpp/mylib.cc
compile _bin/$name cpp/unit_tests.cc \
cpp/frontend_match.cc mycpp/mylib.cc

_bin/$name
}
Expand Down
34 changes: 17 additions & 17 deletions core/error.py
Expand Up @@ -133,31 +133,31 @@ class Runtime(_ErrorWithLocation):
"""A non-fatal runtime error, e.g. for builtins."""


class FatalRuntime(_ErrorWithLocation):
"""An exception that propagates to the top level.
class FatalRuntime(_ErrorWithLocation):
"""An exception that propagates to the top level.
Used in the evaluators, and also also used in test builtin for invalid
argument.
"""
Used in the evaluators, and also also used in test builtin for invalid
argument.
"""


class Strict(FatalRuntime):
"""Depending on shell options, these errors may be caught and ignored.
class Strict(FatalRuntime):
"""Depending on shell options, these errors may be caught and ignored.
For example, if options like these are ON:
For example, if options like these are ON:
set -o strict_arith
set -o strict_word_eval
set -o strict_arith
set -o strict_word_eval
then we re-raise the error so it's caught by the top level. Otherwise
we catch it and return a dummy value like '' or -1 (i.e. what bash commonly
does.)
then we re-raise the error so it's caught by the top level. Otherwise
we catch it and return a dummy value like '' or -1 (i.e. what bash commonly
does.)
TODO: Have levels, like:
TODO: Have levels, like:
OIL_STRICT_PRINT=2 # print warnings at level 2 and above
OIL_STRICT_DIE=1 # abort the program at level 1 and above
"""
OIL_STRICT_PRINT=2 # print warnings at level 2 and above
OIL_STRICT_DIE=1 # abort the program at level 1 and above
"""


if mylib.PYTHON:
Expand Down
14 changes: 13 additions & 1 deletion cpp/core_error.h
Expand Up @@ -15,7 +15,7 @@ using syntax_asdl::word_part_t;
using syntax_asdl::word_t;

// This definition is different in Python than C++. Not worth auto-translating.
class _ErrorWithLocation {
class _ErrorWithLocation : public std::exception {
public:
_ErrorWithLocation(Str* user_str, int span_id)
: user_str_(user_str),
Expand Down Expand Up @@ -89,6 +89,18 @@ class RedirectEval : public _ErrorWithLocation {
}
};

class FatalRuntime: public _ErrorWithLocation {
public:
FatalRuntime(Str* user_str) : _ErrorWithLocation(user_str, -1) {
}
};

class Strict : public FatalRuntime {
public:
Strict(Str* user_str) : FatalRuntime(user_str) {
}
};

// Stub
class ErrExit : public _ErrorWithLocation {
public:
Expand Down
10 changes: 5 additions & 5 deletions cpp/preamble.h
Expand Up @@ -114,24 +114,24 @@ Str* repr(void* obj) {
}

[[noreturn]] void e_strict(Str* s, int span_id) {
assert(0);
throw error::Strict(s);
}

[[noreturn]] void e_strict(Str* s, syntax_asdl::Token* token) {
assert(0);
throw error::Strict(s);
}

[[noreturn]] void e_strict(Str* s, syntax_asdl::word_part_t* part) {
assert(0);
throw error::Strict(s);
}

[[noreturn]] void e_strict(Str* s, syntax_asdl::word_t* w) {
assert(0);
throw error::Strict(s);
}

// Used without args in osh/string_ops.py
[[noreturn]] void e_strict(Str* s) {
assert(0);
throw error::Strict(s);
}

// e.g. used in core/state.py
Expand Down
20 changes: 20 additions & 0 deletions cpp/unit_tests.cc
Expand Up @@ -4,6 +4,7 @@
#include "id.h"
#include "libc.h" // cell, etc
#include "osh_eval_stubs.h" // util::BackslashEscape
#include "preamble.h"
#include "runtime_asdl.h" // cell, etc

TEST show_sizeof() {
Expand Down Expand Up @@ -75,6 +76,24 @@ TEST libc_test() {
PASS();
}

// HACK! asdl/runtime.py isn't translated, but core_error.h uses it...
namespace runtime {
int NO_SPID = -1;
};

TEST exceptions() {
bool caught = false;
try {
e_strict(new Str("foo"));
} catch (error::Strict& e) { // Catch by reference!
//log("%p ", e);
caught = true;
}
ASSERT(caught);

PASS();
}

GREATEST_MAIN_DEFS();

int main(int argc, char **argv) {
Expand All @@ -83,6 +102,7 @@ int main(int argc, char **argv) {
RUN_TEST(match_test);
RUN_TEST(util_test);
RUN_TEST(libc_test);
RUN_TEST(exceptions);
GREATEST_MAIN_END(); /* display results */
return 0;
}

0 comments on commit bdfa6ed

Please sign in to comment.