/*
 * Tests whether a JavaScript value is an array object
 * 
 * This invokes Array.isArray(value) and returns its result
 */
EXPORTAPI bool HyperloopJSValueIsArray(JSContextRef ctx, JSValueRef value) 
{
    if (JSValueIsObject(ctx, value)) 
    {
        JSObjectRef global = JSContextGetGlobalObject(ctx);
        JSValueRef exception = JSValueMakeNull(ctx);
        JSStringRef string = JSStringCreateWithUTF8CString("Array");
        JSObjectRef array = JSValueToObject(ctx, JSObjectGetProperty(ctx, global, string, &exception), &exception);
        JSStringRelease(string);
        if (!JSValueIsNull(ctx, exception)) 
        {
            return false;
        }

        string = JSStringCreateWithUTF8CString("isArray");
        JSObjectRef isArray = JSValueToObject(ctx, JSObjectGetProperty(ctx, array, string, &exception), &exception);
        JSStringRelease(string);
        if (!JSValueIsNull(ctx, exception))
        {
            return false;
        }

        JSValueRef result = JSObjectCallAsFunction(ctx, isArray, global, 1, &value, &exception);

        if (JSValueIsNull(ctx, exception) && JSValueIsBoolean(ctx, result)) 
        {
            return JSValueToBoolean(ctx, result);
        }
    }
    return false;
}
JNIEXPORT jboolean JNICALL
Java_com_appcelerator_hyperloop_ViewOnTouchListener_NativeOnTouch
(JNIEnv *env, jobject thiz, jlong jsContextRef, jlong thisObjectRef, jlong onTouchFuncRef, jobject view, jobject event)
{
    JSContextRef ctx = (JSContextRef)jsContextRef;
    JSObjectRef onTouchFunc = (JSObjectRef)onTouchFuncRef;
    JSObjectRef thisObject = (JSObjectRef)thisObjectRef;
    
    JSValueRef argv[2];
    argv[0] = MakeObjectForJava_android_view_View(ctx, view);
    argv[1] = MakeObjectForJava_android_view_MotionEvent(ctx, event);

    if (JSObjectIsFunction(ctx, onTouchFunc)) {
        JSValueRef exception = JSValueMakeNull(ctx);
        JSValueRef result = JSObjectCallAsFunction(ctx, onTouchFunc, thisObject, 2, argv, &exception);
        if (!JSValueIsNull(ctx, exception)) {
            JSStringRef string = JSValueToStringCopy(ctx, exception, NULL);
            CCHAR_FROM_JSSTRINGREF(string, cstring);
            LOGD("Java_com_appcelerator_hyperloop_ViewOnTouchListener_NativeOnTouch '%s'", cstring);
            free(cstring);
            JSStringRelease(string);
        }
        return JSValueToBoolean(ctx, result) ? JNI_TRUE : JNI_FALSE;
    }
    return JNI_FALSE;
}
JNIEXPORT void JNICALL
Java_com_appcelerator_hyperloop_HyperloopJNI_HyperloopCallActivityOnCreate
(JNIEnv *env, jobject thiz, jlong jsContextRef, jobject activity, jobject savedInstanceState)
{
    JSContextRef context = (JSContextRef)jsContextRef;
    JSObjectRef globalObject = JSContextGetGlobalObject(context);
    
    JSStringRef onCreate = JSStringCreateWithUTF8CString("onCreate");
    JSObjectRef onCreateFunc = JSValueToObject(context,
                    JSObjectGetProperty(context, globalObject, onCreate, NULL), NULL);
    JSStringRelease(onCreate);
    
    // save current Activity
    JSObjectRef activityObj = MakeObjectForJava_android_app_Activity(context, activity);
    
    // save parameter
    JSValueRef args[1];
    args[0] = MakeObjectForJava_android_os_Bundle(context, savedInstanceState);

    JSValueRef exception = JSValueMakeNull(context);
    
    // Call onCreate function
    if (JSObjectIsFunction(context, onCreateFunc)) {
        JSObjectCallAsFunction(context, onCreateFunc, activityObj, 1, args, &exception);
    }
    if (!JSValueIsNull(context, exception)) {
        JSStringRef string = JSValueToStringCopy(context, exception, NULL);
        CCHAR_FROM_JSSTRINGREF(string, cstring);
        LOGD("Java_com_appcelerator_hyperloop_HyperloopJNI_HyperloopCallActivityOnCreate '%s'", cstring);
        free(cstring);
        JSStringRelease(string);
    }
    
}
Beispiel #4
0
Type GetType(JSValueRef value)
{
    if (JSValueIsNull(g_ctx, value))
        return VTYPE_NULL;
    if (JSValueIsBoolean(g_ctx, value))
        return VTYPE_BOOL;
    if (JSValueIsNumber(g_ctx, value))
        return VTYPE_DOUBLE;
    if (JSValueIsString(g_ctx, value))
        return VTYPE_STRING;

    if (JSValueIsObject(g_ctx, value))
    {
        JSObjectRef obj = JSValueToObject(g_ctx, value, NULL);

        if (JSObjectIsFunction(g_ctx, obj))
            return VTYPE_FUNCTION;
        if (IsArray(obj))
            return VTYPE_LIST;

        return VTYPE_DICTIONARY;
    }

    return VTYPE_INVALID;
}
Beispiel #5
0
bool WebViewTest::javascriptResultIsNull(WebKitJavascriptResult* javascriptResult)
{
    JSGlobalContextRef context = webkit_javascript_result_get_global_context(javascriptResult);
    g_assert(context);
    JSValueRef value = webkit_javascript_result_get_value(javascriptResult);
    g_assert(value);

    return JSValueIsNull(context, value);
}
Beispiel #6
0
static JSValueRef
seed_gobject_signal_connect_on_property (JSContextRef ctx,
					 JSObjectRef function,
					 JSObjectRef thisObject,
					 size_t argumentCount,
					 const JSValueRef arguments[],
					 JSValueRef * exception)
{
  gulong id = 0;
  JSObjectRef this_obj;
  signal_privates *privates;

  privates = (signal_privates *) JSObjectGetPrivate (thisObject);
  if (!privates)
    g_error ("Signal constructed with invalid parameters"
	     "in namespace import \n");

  this_obj =
    (JSObjectRef) seed_value_from_object (ctx, privates->object, exception);

  if ((argumentCount > 2) || (argumentCount == 0))
    {
      seed_make_exception (ctx, exception, "ArgumentError",
			   "Signal connection expected"
			   " 1, or 2 arguments. Got " "%zd", argumentCount);

      return JSValueMakeNull (ctx);
    }

  if (JSValueIsNull (ctx, arguments[0]) ||
      !JSValueIsObject (ctx, arguments[0]) ||
      !JSObjectIsFunction (ctx, (JSObjectRef) arguments[0]))
    {
      seed_make_exception (ctx, exception, "ArgumentError",
			   "Signal connection requires a function"
			   " as first argument");
      return JSValueMakeNull (ctx);
    }

  if (argumentCount == 1)
    {
      id = seed_gobject_signal_connect (ctx, privates->signal_name,
					privates->object,
					(JSObjectRef) arguments[0], this_obj,
					NULL);

    }
  else if (argumentCount == 2)
    {
      id = seed_gobject_signal_connect (ctx, privates->signal_name,
					privates->object,
					(JSObjectRef) arguments[0],
					this_obj, (JSObjectRef) arguments[1]);
    }

  return seed_value_from_ulong (ctx, id, exception);
}
Beispiel #7
0
static JSValueRef
seed_gobject_signal_connect_by_name (JSContextRef ctx,
				     JSObjectRef function,
				     JSObjectRef thisObject,
				     size_t argumentCount,
				     const JSValueRef arguments[],
				     JSValueRef * exception)
{
  GType obj_type;
  JSObjectRef user_data = NULL;
  gchar *signal_name;
  GObject *obj;
  gulong id;

  if (argumentCount < 2 || argumentCount > 3)
    {
      seed_make_exception (ctx, exception, "ArgumentError",
			   "Signal connection expected"
			   " 2 or 3 arguments. Got " "%zd", argumentCount);

      return JSValueMakeNull (ctx);
    }

  if (JSValueIsNull (ctx, arguments[1]) ||
      !JSValueIsObject (ctx, arguments[1]) ||
      !JSObjectIsFunction (ctx, (JSObjectRef) arguments[1]))
    {
      seed_make_exception (ctx, exception, "ArgumentError",
			   "Signal connection by name "
			   "requires a function" " as second argument");
      return JSValueMakeNull (ctx);
    }

  if (argumentCount == 3)
    {
      user_data = (JSObjectRef) arguments[2];
    }

  signal_name = seed_value_to_string (ctx, arguments[0], exception);
  obj = (GObject *) JSObjectGetPrivate (thisObject);
  obj_type = G_OBJECT_TYPE (obj);

  id = seed_gobject_signal_connect (ctx, signal_name, obj,
				    (JSObjectRef) arguments[1], NULL,
				    user_data);

  g_free (signal_name);

  return seed_value_from_ulong (ctx, id, exception);
}
Beispiel #8
0
JSStringRef to_string(JSContextRef ctx, JSValueRef val) {
    if (JSValueIsUndefined(ctx, val)) {
        return JSStringCreateWithUTF8CString("undefined");
    } else if (JSValueIsNull(ctx, val)) {
        return JSStringCreateWithUTF8CString("null");
    } else {
        JSStringRef to_string_name = JSStringCreateWithUTF8CString("toString");
        JSObjectRef obj = JSValueToObject(ctx, val, NULL);
        JSValueRef to_string = JSObjectGetProperty(ctx, obj, to_string_name, NULL);
        JSObjectRef to_string_obj = JSValueToObject(ctx, to_string, NULL);
        JSValueRef obj_val = JSObjectCallAsFunction(ctx, to_string_obj, obj, 0, NULL, NULL);

        return JSValueToStringCopy(ctx, obj_val, NULL);
    }
}
/**
 * internal
 * 
 * implementation of console.log 
 */
static JSValueRef HyperloopLogger (JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
    if (argumentCount>0) 
    {
        std::ostringstream stream;
        for (size_t c=0;c<argumentCount;c++)
        {
            if (JSValueIsObject(ctx,arguments[c]) || JSValueIsString(ctx,arguments[c])) 
            {
                std::string str(HyperloopJSValueToStringCopy(ctx,arguments[c],exception));
                stream << str;
            }
            else if (JSValueIsNumber(ctx,arguments[c]))
            {
                double num = JSValueToNumber(ctx,arguments[c],exception);
                double intpart;
                if (modf(num, &intpart) == 0.0)
                {
                    stream << intpart;
                }
                else 
                {
                    stream << num;
                }
            }
            else if (JSValueIsBoolean(ctx,arguments[c]))
            {
                bool b = JSValueToBoolean(ctx,arguments[c]);
                stream << (b ? "true":"false");
            }
            else if (JSValueIsNull(ctx,arguments[c]))
            {
                stream << "null";
            }
            else if (JSValueIsUndefined(ctx,arguments[c]))
            {
                stream << "undefined";
            }
            if (c+1 < argumentCount) 
            {
                stream << " ";
            }
        }
        // call the platform adapter
        HyperloopNativeLogger(stream.str().c_str());
    }
    return JSValueMakeUndefined(ctx);
}
Beispiel #10
0
JS_EXPORT_API
void dock_draw_window_preview(JSValueRef canvas, double xid, double dest_width, double dest_height)
{
    GdkWindow* win = gdk_x11_window_foreign_new_for_display(gdk_display_get_default(), (long)xid);
    if (win == NULL) {
	return;
    }

    if (JSValueIsNull(get_global_context(), canvas)) {
        g_debug("draw_window_preview with null canvas!");
        return;
    }
    cairo_t* cr =  fetch_cairo_from_html_canvas(get_global_context(), canvas);

    if (cr == NULL) {
        return;
    }

    cairo_save(cr);
    //clear preview content to prevent translucency window problem
    cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
    cairo_paint(cr);
    cairo_restore(cr);

    int width = gdk_window_get_width(win);
    int height = gdk_window_get_height(win);

    cairo_save(cr);
    double scale = 0;
    if (width > height) {
        scale = dest_width/width;
        cairo_scale(cr, scale, scale);
        gdk_cairo_set_source_window(cr, win, 0, 0.5*(dest_height/scale-height));
    } else {
        scale = dest_height/height;
        cairo_scale(cr, scale, scale);
        gdk_cairo_set_source_window(cr, win, 0.5*(dest_width/scale-width), 0);
    }
    cairo_paint(cr);
    cairo_restore(cr);

    canvas_custom_draw_did(cr, NULL);
}
Beispiel #11
0
char *value_to_c_string(JSContextRef ctx, JSValueRef val) {
    if (JSValueIsNull(ctx, val)) {
        return NULL;
    }

    if (!JSValueIsString(ctx, val)) {
#ifdef DEBUG
        fprintf(stderr, "WARN: not a string\n");
#endif
        return NULL;
    }

    JSStringRef str_ref = JSValueToStringCopy(ctx, val, NULL);
    size_t len = JSStringGetMaximumUTF8CStringSize(str_ref);
    char *str = malloc(len * sizeof(char));
    JSStringGetUTF8CString(str_ref, str, len);
    JSStringRelease(str_ref);

    return str;
}
Beispiel #12
0
JSValueRef function_http_request(JSContextRef ctx, JSObjectRef function, JSObjectRef this_object,
		size_t argc, const JSValueRef args[], JSValueRef* exception) {
	if (argc == 1 && JSValueGetType(ctx, args[0]) == kJSTypeObject) {
		JSObjectRef opts = JSValueToObject(ctx, args[0], NULL);
		JSValueRef url_ref = JSObjectGetProperty(ctx, opts, JSStringCreateWithUTF8CString("url"), NULL);
		char *url = value_to_c_string(ctx, url_ref);
		JSValueRef timeout_ref = JSObjectGetProperty(ctx, opts, JSStringCreateWithUTF8CString("timeout"), NULL);
		time_t timeout = 0;
		if (JSValueIsNumber(ctx, timeout_ref)) {
			timeout = (time_t) JSValueToNumber(ctx, timeout_ref, NULL);
		}
		JSValueRef method_ref = JSObjectGetProperty(ctx, opts, JSStringCreateWithUTF8CString("method"), NULL);
		char *method = value_to_c_string(ctx, method_ref);
		JSValueRef body_ref = JSObjectGetProperty(ctx, opts, JSStringCreateWithUTF8CString("body"), NULL);

		JSObjectRef headers_obj = JSValueToObject(ctx, JSObjectGetProperty(ctx, opts, JSStringCreateWithUTF8CString("headers"), NULL), NULL);

		CURL *handle = curl_easy_init();
		assert(handle != NULL);

		curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, method);
		curl_easy_setopt(handle, CURLOPT_URL, url);

		struct curl_slist *headers = NULL;
		if (!JSValueIsNull(ctx, headers_obj)) {
			JSPropertyNameArrayRef properties = JSObjectCopyPropertyNames(ctx, headers_obj);
			size_t n = JSPropertyNameArrayGetCount(properties);
			for (int i = 0; i < n; i++) {
				JSStringRef key_str = JSPropertyNameArrayGetNameAtIndex(properties, i);
				JSValueRef val_ref = JSObjectGetProperty(ctx, headers_obj, key_str, NULL);

				size_t len = JSStringGetLength(key_str) + 1;
				char *key = malloc(len * sizeof(char));
				JSStringGetUTF8CString(key_str, key, len);
				JSStringRef val_as_str = to_string(ctx, val_ref);
				char *val = value_to_c_string(ctx, JSValueMakeString(ctx, val_as_str));
				JSStringRelease(val_as_str);

				size_t len_key = strlen(key);
				size_t len_val = strlen(val);
				char *header = malloc((len_key + len_val + 2 + 1) * sizeof(char));
				sprintf(header, "%s: %s", key, val);
				curl_slist_append(headers, header);
				free(header);

				free(key);
				free(val);
			}

			curl_easy_setopt(handle, CURLOPT_HEADER, headers);
		}

		// curl_easy_setopt(handle, CURLOPT_HEADER, 1L);
		curl_easy_setopt(handle, CURLOPT_TIMEOUT, timeout);

		struct read_string_state input_state;
		if (!JSValueIsUndefined(ctx, body_ref)) {
			char *body = value_to_c_string(ctx, body_ref);
			input_state.input = body;
			input_state.offset = 0;
			input_state.length = strlen(body);
			curl_easy_setopt(handle, CURLOPT_READDATA, &input_state);
			curl_easy_setopt(handle, CURLOPT_READFUNCTION, read_string_callback);
		}

		JSObjectRef response_headers = JSObjectMake(ctx, NULL, NULL);
		struct header_state header_state;
		header_state.ctx = ctx;
		header_state.headers = response_headers;
		curl_easy_setopt(handle, CURLOPT_HEADERDATA, &header_state);
		curl_easy_setopt(handle, CURLOPT_HEADERFUNCTION, header_to_object_callback);

		struct write_state body_state;
		body_state.offset = 0;
		body_state.length = 0;
		body_state.data = NULL;
		curl_easy_setopt(handle, CURLOPT_WRITEDATA, &body_state);
		curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_string_callback);

		JSObjectRef result = JSObjectMake(ctx, NULL, NULL);

		int res = curl_easy_perform(handle);
		if (res != 0) {
			JSStringRef error_str = JSStringCreateWithUTF8CString(curl_easy_strerror(res));
			JSObjectSetProperty(ctx, result, JSStringCreateWithUTF8CString("error"), JSValueMakeString(ctx, error_str), kJSPropertyAttributeReadOnly, NULL);
		}

		int status = 0;
		curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &status);

		// printf("%d bytes, %x\n", body_state.offset, body_state.data);
		if (body_state.data != NULL) {
			JSStringRef body_str = JSStringCreateWithUTF8CString(body_state.data);
			JSObjectSetProperty(ctx, result, JSStringCreateWithUTF8CString("body"), JSValueMakeString(ctx, body_str), kJSPropertyAttributeReadOnly, NULL);
			free(body_state.data);
		}

		JSObjectSetProperty(ctx, result, JSStringCreateWithUTF8CString("status"), JSValueMakeNumber(ctx, status), kJSPropertyAttributeReadOnly, NULL);
		JSObjectSetProperty(ctx, result, JSStringCreateWithUTF8CString("headers"), response_headers, kJSPropertyAttributeReadOnly, NULL);

		curl_slist_free_all(headers);
		curl_easy_cleanup(handle);

		return result;
	}

	return JSValueMakeNull(ctx);
}
Beispiel #13
0
void
seed_signal_marshal_func (GClosure * closure,
			  GValue * return_value,
			  guint n_param_values,
			  const GValue * param_values,
			  gpointer invocation_hint, gpointer marshal_data)
{
  SeedClosure *seed_closure = (SeedClosure *) closure;
  JSValueRef *args, exception = 0;
  JSValueRef ret = 0;
  guint i;
  gchar *mes;
  GSignalQuery signal_query = { 0, };

  if (marshal_data)
    {
      /* Inspired from gjs/gi/value.c:closure_marshal() */
      /* we are used for a signal handler */
      guint signal_id;

      signal_id = GPOINTER_TO_UINT(marshal_data);

      g_signal_query(signal_id, &signal_query);

      if (!signal_query.signal_id)
          g_error("Signal handler being called on invalid signal");

      if (signal_query.n_params + 1 != n_param_values)
          g_error("Signal handler being called with wrong number of parameters");
  }

  JSContextRef ctx = JSGlobalContextCreateInGroup (context_group,
						   0);

  seed_prepare_global_context (ctx);
  SEED_NOTE (INVOCATION, "Signal Marshal: ");

  args = g_newa (JSValueRef, n_param_values + 1);

  for (i = 0; i < n_param_values; i++)
    {
      args[i] = seed_value_from_gvalue_for_signal (ctx,
                    (GValue *) & param_values[i], 0, &signal_query, i);

      if (!args[i])
	g_error ("Error in signal marshal. "
		 "Unable to convert argument of type: %s \n",
		 g_type_name (param_values[i].g_type));

    }

  if (seed_closure->user_data)
    args[i] = seed_closure->user_data;
  else
    args[i] = JSValueMakeNull (ctx);

  ret = JSObjectCallAsFunction (ctx, seed_closure->function,
				NULL, n_param_values + 1, args, &exception);

  if (exception)
    {
      seed_closure_warn_exception (closure, ctx, exception);
      exception = NULL;
    }

  if (ret && !JSValueIsNull (ctx, ret)
      && (seed_closure->return_type != G_TYPE_NONE))
    {
      seed_value_to_gvalue (ctx, ret, seed_closure->return_type,
			    return_value, &exception);
    }

  if (exception)
    {
      mes = seed_exception_to_string (ctx, exception);
      g_warning ("Exception in signal handler return value. %s \n", mes);
      g_free (mes);
    }

  JSGlobalContextRelease ((JSGlobalContextRef) ctx);
  JSGarbageCollect(ctx);

}
Beispiel #14
0
void _draw(JSValueRef canvas, double dest_width, double dest_height)
{
    static gboolean not_draw = FALSE;

    if (recognition_info.reco_state == RECOGNIZING) {
        /* g_debug("[%s] recognizing", __func__); */
        return;
    }

    if (!recognition_info.has_data) {
        g_debug("[%s] get no data from camera", __func__);
        return;
    }

    if (JSValueIsNull(get_global_context(), canvas)) {
        g_debug("[%s] draw with null canvas!", __func__);
        return;
    }

    if (recognition_info.source_data == NULL) {
        g_debug("[%s] source_data is null", __func__);
        return;
    }

    g_debug("[%s]", __func__);

    cairo_t* cr = fetch_cairo_from_html_canvas(get_global_context(), canvas);
    g_assert(cr != NULL);
    cairo_save(cr);

    gulong len = recognition_info.length;
    guchar* data = g_slice_copy(len, recognition_info.source_data);
    GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data(data,
                                                 GDK_COLORSPACE_RGB,  // color space
                                                 FALSE,  // has alpha
                                                 8,  // bits per sample
                                                 CAMERA_WIDTH,  // width
                                                 CAMERA_HEIGHT,  // height
                                                 3*CAMERA_WIDTH,  // row stride
                                                 destroy_pixels,  // destroy function
                                                 GINT_TO_POINTER(len)  // destroy function data
                                                );

    double scale = 0;
    if (CAMERA_WIDTH > CAMERA_HEIGHT) {
        scale = dest_height/CAMERA_HEIGHT;
        cairo_scale(cr, scale, scale);
        gdk_cairo_set_source_pixbuf(cr, pixbuf, 0.5 * (dest_width / scale -
                                                       CAMERA_WIDTH), 0);
    } else {
        scale = dest_width/CAMERA_WIDTH;
        cairo_scale(cr, scale, scale);
        gdk_cairo_set_source_pixbuf(cr, pixbuf, 0, 0.5 * (dest_height / scale -
                                                          CAMERA_HEIGHT));
    }

    cairo_paint(cr);
    cairo_restore(cr);

    canvas_custom_draw_did(cr, NULL);
    g_object_unref(pixbuf);

    recognition_info.has_data = FALSE;
}
Beispiel #15
0
template<> bool RJSAccessor::is_null(JSContextRef ctx, JSValueRef &val) {
    return JSValueIsNull(ctx, val) || JSValueIsUndefined(ctx, val);
}
Beispiel #16
0
bool jsc::Value::isNull()      const {return JSValueIsNull(context, value);}