Ai
1 Star 1 Fork 0

ScSofts/quickjspp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
exception.cpp 1.92 KB
一键复制 编辑 原始数据 按行查看 历史
ftk 提交于 2020-06-15 18:36 +08:00 . Fix #13
#include "quickjspp.hpp"
#include <iostream>
struct A{};
int main()
{
qjs::Runtime runtime;
qjs::Context context(runtime);
context.global().add("println", [](const std::string& s) { std::cout << s << std::endl; });
context.registerClass<A>("A");
context.registerClass<A>("A");
try
{
auto obj = context.eval("Symbol.toPrimitive");
std::cout << static_cast<int>(obj) << std::endl;
assert(false);
}
catch(qjs::exception)
{
auto exc = context.getException();
assert(exc.isError() && (std::string) exc == "TypeError: cannot convert symbol to number");
}
try
{
std::string big;
big.resize((1 << 30) + 1);
auto fun = context.eval("(function(a, b, c) { println(a); println(b); println(c); })").as<std::function<void(double, std::string, int)>>();
fun(0, std::move(big), 0);
assert(false);
}
catch(qjs::exception)
{
auto exc = context.getException();
std::cout << (std::string) exc << '\n' << (std::string_view) exc["stack"];
}
try
{
//context.global().add("emptyf", [](JSValue v) {} );
auto f = (std::function<void ()>) context.eval("(function() { +Symbol.toPrimitive })");
f();
assert(false);
}
catch(qjs::exception)
{
auto exc = context.getException();
std::cout << (std::string) exc << '\n' << (std::string_view) exc["stack"];
}
try
{
qjs::Value function = context.eval("() => { let a = b; }", "<test>");
auto native = function.as<std::function<qjs::Value()>>();
qjs::Value result = native();
assert(false);
}
catch(qjs::exception)
{
auto exc = context.getException();
std::cerr << (exc.isError() ? "Error: " : "Throw: ") << (std::string)exc << std::endl;
if((bool)exc["stack"])
std::cerr << (std::string)exc["stack"] << std::endl;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/gitnow/quickjspp.git
git@gitee.com:gitnow/quickjspp.git
gitnow
quickjspp
quickjspp
master

搜索帮助