std::string Value::toJSONString(unsigned indent) const { JSValueRef exn; auto stringToAdopt = JSC_JSValueCreateJSONString(m_context, m_value, indent, &exn); if (!stringToAdopt) { throw JSException(m_context, exn, "Exception creating JSON string"); } return String::adopt(m_context, stringToAdopt).str(); }
void Object::setProperty(const String& propName, const Value& value) { JSValueRef exn = nullptr; JSC_JSObjectSetProperty(m_context, m_obj, propName, value, kJSPropertyAttributeNone, &exn); if (exn) { throw JSException(m_context, exn, folly::to<std::string>( "Failed to set property '", propName.str(), "'").c_str()); } }
void Object::setPropertyAtIndex(unsigned int index, const Value& value) { JSValueRef exn = nullptr; JSC_JSObjectSetPropertyAtIndex(m_context, m_obj, index, value, &exn); if (exn) { throw JSException(m_context, exn, folly::to<std::string>( "Failed to set property at index ", index).c_str()); } }
Value Object::callAsFunction(JSObjectRef thisObj, int nArgs, const JSValueRef args[]) const { JSValueRef exn; JSValueRef result = JSC_JSObjectCallAsFunction(m_context, m_obj, thisObj, nArgs, args, &exn); if (!result) { throw JSException(m_context, exn, "Exception calling object as function"); } return Value(m_context, result); }
Object Object::callAsConstructor(std::initializer_list<JSValueRef> args) const { JSValueRef exn; JSObjectRef result = JSC_JSObjectCallAsConstructor(m_context, m_obj, args.size(), args.begin(), &exn); if (!result) { throw JSException(m_context, exn, "Exception calling object as constructor"); } return Object(m_context, result); }
String Value::toString() const { JSValueRef exn; JSStringRef jsStr = JSC_JSValueToStringCopy(context(), m_value, &exn); if (!jsStr) { throw JSException(m_context, exn, "Failed to convert to string"); } return String::adopt(context(), jsStr); }
Object Value::asObject() const { JSValueRef exn; JSObjectRef jsObj = JSC_JSValueToObject(context(), m_value, &exn); if (!jsObj) { throw JSException(m_context, exn, "Failed to convert to object"); } return Object(context(), jsObj); }
JSValueRef evaluateSourceCode(JSContextRef context, JSSourceCodeRef source, JSStringRef sourceURL) { JSValueRef exn, result; result = JSEvaluateSourceCode(context, source, NULL, &exn); if (result == nullptr) { throw JSException(context, exn, sourceURL); } return result; }
JSValueRef evaluateScript(JSContextRef context, JSStringRef script, JSStringRef sourceURL) { JSValueRef exn, result; result = JSC_JSEvaluateScript(context, script, NULL, sourceURL, 0, &exn); if (result == nullptr) { throw JSException(context, exn, sourceURL); } return result; }
/* static */ Value Value::fromJSON(JSContextRef ctx, const String& json) { auto result = JSC_JSValueMakeFromJSONString(ctx, json); if (!result) { throw JSException(folly::to<std::string>( "Failed to create Value from JSON: ", json.str()).c_str()); } return Value(ctx, result); }
Value Object::getPropertyAtIndex(unsigned int index) const { JSValueRef exn; JSValueRef property = JSC_JSObjectGetPropertyAtIndex(m_context, m_obj, index, &exn); if (!property) { throw JSException(m_context, exn, folly::to<std::string>( "Failed to get property at index ", index).c_str()); } return Value(m_context, property); }
Value Object::getProperty(const String& propName) const { JSValueRef exn; JSValueRef property = JSC_JSObjectGetProperty(m_context, m_obj, propName, &exn); if (!property) { throw JSException(m_context, exn, folly::to<std::string>( "Failed to get property '", propName.str(), "'").c_str()); } return Value(m_context, property); }
Value Value::makeError(JSContextRef ctx, const char *error) { JSValueRef exn; JSValueRef args[] = { Value(ctx, String(ctx, error)) }; JSObjectRef errorObj = JSC_JSObjectMakeError(ctx, 1, args, &exn); if (!errorObj) { throw JSException(ctx, exn, "Exception making error"); } return Value(ctx, errorObj); }
Value Value::makeError(JSContextRef ctx, const char *error, const char *stack) { auto errorMsg = Value(ctx, String(ctx, error)); JSValueRef args[] = {errorMsg}; if (stack) { // Using this instead of JSObjectMakeError to actually get a stack property. // MakeError only sets it stack when returning from the invoked function, so we // can't extend it here. auto errorConstructor = Object::getGlobalObject(ctx).getProperty("Error").asObject(); auto jsError = errorConstructor.callAsConstructor({errorMsg}); auto fullStack = std::string(stack) + jsError.getProperty("stack").toString().str(); jsError.setProperty("stack", String(ctx, fullStack.c_str())); return jsError; } else { JSValueRef exn; JSObjectRef errorObj = JSC_JSObjectMakeError(ctx, 1, args, &exn); if (!errorObj) { throw JSException(ctx, exn, "Exception making error"); } return Value(ctx, errorObj); } }