Mercurial > code
view C++/modules/Js/Js.cpp @ 402:c21f37c9cd4a
Js:
- Add Context::optional
- Add Context::require
author | David Demelier <markand@malikania.fr> |
---|---|
date | Mon, 05 Oct 2015 13:22:20 +0200 |
parents | ca5e4360f79a |
children | 2a0b8613498f |
line wrap: on
line source
/* * Js.cpp -- JavaScript C++14 wrapper for Duktape * * Copyright (c) 2013-2015 David Demelier <markand@malikania.fr> * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include "Js.h" using namespace std::string_literals; namespace js { ErrorInfo Context::error(int index) { ErrorInfo error; index = duk_normalize_index(m_handle.get(), index); duk_get_prop_string(m_handle.get(), index, "name"); error.name = duk_to_string(m_handle.get(), -1); duk_get_prop_string(m_handle.get(), index, "message"); error.message = duk_to_string(m_handle.get(), -1); duk_get_prop_string(m_handle.get(), index, "fileName"); error.fileName = duk_to_string(m_handle.get(), -1); duk_get_prop_string(m_handle.get(), index, "lineNumber"); error.lineNumber = duk_to_int(m_handle.get(), -1); duk_get_prop_string(m_handle.get(), index, "stack"); error.stack = duk_to_string(m_handle.get(), -1); duk_pop_n(m_handle.get(), 5); return error; } void Context::pcall(unsigned nargs) { if (duk_pcall(m_handle.get(), nargs) != 0) { ErrorInfo info = error(-1); duk_pop(m_handle.get()); throw info; } } void Context::peval() { if (duk_peval(m_handle.get()) != 0) { ErrorInfo info = error(-1); duk_pop(m_handle.get()); throw info; } } } // !js