#include "JS.hpp" #include "JSON.hpp" #include #include namespace JS { Value getGlobal(char const* name) { return Value::global(name); } Value getGlobal(std::string const& name) { return Value::global(name.c_str()); } Value getConstant(const std::string& name) { return getGlobal(name); } bool isInstanceOf(const Value& value, const char* name) { return value.instanceof (JS::Value::global(name)); } std::vector jsArrayToVector(const Value& array) { std::vector result; int size = array["length"].as(); result.reserve(size); for (int i = 0; i < size; ++i) result.emplace_back(array[i]); return result; } std::map jsObjectToMap(const Value& object) { std::map result; Value keys = gObject.call("keys", object); int size = keys["length"].as(); for (int i = 0; i < size; ++i) { auto key = keys[i].as(); result.emplace_hint(result.end(), key, object[key]); } return result; } Value fromJSONArray(const JSON& array) { Value value = Value::array(); for (std::size_t i = 0; i < array.size(); i++) value.set(i, fromJSON(array[i])); return value; } Value fromJSONObject(const JSON& object) { Value value = Value::object(); for (auto iter = object.begin(); iter != object.end(); ++iter) value.set(iter.key(), fromJSON(iter.value())); return value; } Value fromJSON(const JSON& value) { if (value.is_object()) return fromJSONObject(value); else if (value.is_array()) return fromJSONArray(value); else if (value.is_boolean()) return JS::Value(value.get()); else if (value.is_number_unsigned()) return JS::Value(value.get()); else if (value.is_number_integer()) return JS::Value(value.get()); else if (value.is_number_float()) return JS::Value(value.get()); else if (value.is_string()) return JS::Value(value.get()); else return JS::Value::null(); } JSON toJSON(const JS::Value& value) { return JSON::parse(JS::getGlobal("JSON").call("stringify", value)); } Console::Console() : object_(JS::Value::global()["console"]) { } Console console; } // namespace JS