Example #1
0
/* static */
Value Value::fromJSON(JSContextRef ctx, const String& json) {
  auto result = JSValueMakeFromJSONString(ctx, json);
  if (!result) {
    throwJSExecutionException("Failed to create String from JSON");
  }
  return Value(ctx, result);
}
Example #2
0
void JSCExecutor::setGlobalVariable(const std::string& propName, const std::string& jsonValue) {
  auto globalObject = JSContextGetGlobalObject(m_context);
  String jsPropertyName(propName.c_str());

  String jsValueJSON(jsonValue.c_str());
  auto valueToInject = JSValueMakeFromJSONString(m_context, jsValueJSON);

  JSObjectSetProperty(m_context, globalObject, jsPropertyName, valueToInject, 0, NULL);
}
Example #3
0
bool script_state_load_single(int script_idx,bool checkpoint,char *err_str)
{
	int				prop_name_len,prop_value_len;
	char			prop_name[256];
	char			*prop_value;
	script_type		*script;
	JSStringRef		js_prop_name,js_prop_json;
	JSValueRef		js_prop_value;

	if (script_idx==-1) return(TRUE);

		// get the script

	script=js.script_list.scripts[script_idx];

		// run through the properties to replace
		// a 0 property name length means the end
		// of properties for this script

	while (TRUE) {

			// get the prop name and JSON value

		game_file_get_chunk(&prop_name_len);
		if (prop_name_len==0) break;

		game_file_get_chunk(prop_name);

		game_file_get_chunk(&prop_value_len);
		prop_value=(char*)malloc(prop_value_len);
		if (prop_value==NULL) {
			strcpy(err_str,"Out of Memory");
			return(FALSE);
		}

		game_file_get_chunk(prop_value);

			// process the property

		js_prop_name=JSStringCreateWithUTF8CString(prop_name);

		js_prop_json=JSStringCreateWithUTF8CString(prop_value);
		js_prop_value=JSValueMakeFromJSONString(script->cx,js_prop_json);
		JSStringRelease(js_prop_json);

		JSObjectSetProperty(script->cx,script->global_obj,js_prop_name,js_prop_value,0,NULL);
	
		JSStringRelease(js_prop_name);
	}

		// send load event to script

	scripts_post_event(script_idx,-1,sd_event_state,(checkpoint?sd_event_state_load_checkpoint:sd_event_state_load),0,err_str);

	return(TRUE);
}
Example #4
0
// has property method for callback parameters
JSValueRef spjsdata_get_property_cb(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
{
    bool ret = false;
    JSValueRef retval;
    int propertySize = JSStringGetMaximumUTF8CStringSize(propertyName);
    char* property = (char*)malloc(propertySize*sizeof(char));
    JSStringGetUTF8CString(propertyName, property, propertySize);
    char* data = (char*)JSObjectGetPrivate(object);
    if(strcmp(kParamOTP,property) == 0)
    {

        JSStringRef sref = JSStringCreateWithUTF8CString(data);
		JSValueRef ref = JSValueMakeString(ctx, sref);
        retval = ref;
        ret = true;
    }
    if(strcmp(kParamCode,property) == 0)
    {
        JSStringRef sref = JSStringCreateWithUTF8CString(data);
		JSValueRef ref = JSValueMakeString(ctx, sref);
        retval = ref;
        ret = true;
    }
    if(strcmp(kParamMessage,property) == 0)
    {
        JSStringRef sref = JSStringCreateWithUTF8CString(data);
		JSValueRef ref = JSValueMakeString(ctx, sref);
        retval = ref;
        ret = true;
    }
    if(strcmp(kParamData,property) == 0)
    {
        //JSStringRef sref = JSStringCreateWithUTF8CString(data);
  //JSValueRef ref = JSValueMakeString(ctx, sref);
        
        // json object is passed to e.data
        DPInfo* info = (DPInfo*)JSObjectGetPrivate(object);
//        std::string json = "{\"status\":\"" + (char)info->status + "\",\"version\":\"" + info->version + "\",\"serial\":\"" + info->serial + "\"}";

        char *jsonBuffer = new char[256];
        memset(jsonBuffer, 0, 256);
        sprintf(jsonBuffer, "{\"status\":\"%u\",\"version\":\"%u\",\"serial\":\"%s\",\"passwordFatalCounter\":\"%u\",\"appEventCounter\":\"%lu\"}", info->status, info->version, info->serial.c_str(), info->fatalCounter, info->appEventCounter);
        JSStringRef sref = JSStringCreateWithUTF8CString(jsonBuffer);
        delete []jsonBuffer;

        JSValueRef ref = JSValueMakeFromJSONString(ctx, sref);
        retval = ref;
        ret = true;
    }

    free(property);
    return retval;
}
Example #5
0
 JSValue::JSValue(const JSContext& js_context, const JSString& js_string, bool parse_as_json)
 : js_context__(js_context) {
   HAL_LOG_TRACE("JSValue:: ctor 1 ", this);
   if (parse_as_json) {
     js_value_ref__ = JSValueMakeFromJSONString(static_cast<JSContextRef>(js_context), static_cast<JSStringRef>(js_string));
     if (!js_value_ref__) {
       const std::string message = "Input is not a valid JSON string: " + to_string(js_string);
       detail::ThrowRuntimeError("JSValue", message);
     }
   } else {
     js_value_ref__ = JSValueMakeString(static_cast<JSContextRef>(js_context__), static_cast<JSStringRef>(js_string));
   }
   HAL_LOG_TRACE("JSValue:: retain ", js_value_ref__, " for ", this);
   JSValueProtect(static_cast<JSContextRef>(js_context__), js_value_ref__);
 }
Example #6
0
NATIVE(JSValue,jlong,makeFromJSONString) (PARAMS, jlong ctx, jlong stringRef)
{
	JSContextWrapper *wrapper = (JSContextWrapper *)ctx;
	struct msg_t {
		JSContextRef ctx;
		JSStringRef  stringRef;
		JSValueRef   valueRef;
	};
	msg_t msg = {
		wrapper->context,
		(JSStringRef)stringRef,
		(JSValueRef)0
        };
	wrapper->worker_q->sync([](void *msg){
		msg_t *m = (msg_t *)msg;
		m->valueRef = JSValueMakeFromJSONString(m->ctx, m->stringRef);
	}, &msg);
	return (long) msg.valueRef;
}