static JSBool
jsExec( JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval )
{
   if( ( 1 <= argc )
       &&
       JSVAL_IS_STRING( argv[0] ) ){
      execCalled_ = true ;
      abortCodeQueue();
      execCmd_ = JS_GetStringBytes( JS_ValueToString( cx, argv[0] ) );
      execCmdArgs_.clear();
      execCmdArgs_.push_back(execCmd_);
   }
   else if( (1 == argc) && JSVAL_IS_OBJECT(argv[0]) ){
      JSObject  *obj ;
      JSIdArray *elements ;

      if( JS_ValueToObject(cx, argv[0], &obj)
          &&
          ( 0 != ( elements = JS_Enumerate(cx,obj ) ) ) ){
         for( int i = 0 ; i < elements->length ; i++ )
         {
            jsval rv ;
            JSString *s ;
            if( JS_LookupElement( cx, obj, i, &rv )    //look up each element
                &&
                (0 != (s=JS_ValueToString(cx, rv))) )
            {
               execCmdArgs_.push_back(std::string(JS_GetStringBytes(s)));
            }
         }
      }
      else
         JS_ReportError(cx, "Error parsing exec() array parameter\n" );
      
      if( 0 < execCmdArgs_.size() ){
         execCmd_ = execCmdArgs_[0];
         execCalled_ = true ;
      }
   }
   else
      JS_ReportError( cx, "Usage: exec( 'cmdline' ) || exec( ['/path/to/exe','param1' ...] )\n" );

   *rval = JSVAL_TRUE ;
   return JS_TRUE ;
}
Beispiel #2
0
/* Copy an array from JS to Java;  Create a new Java array and populate its
   elements, one by one, with the result of converting each JS array element
   to the type of the array component. */
static JSBool
convert_js_array_to_java_array(JSContext *cx, JNIEnv *jEnv, JSObject *js_array,
                               JavaSignature *signature,
                               jobject *java_valuep)
{
    jsuint i;
    jsval js_val;
    jsuint length;
    jclass component_class;
    jarray java_array;
    JavaSignature *array_component_signature;

    if (!JS_GetArrayLength(cx, js_array, &length))
        return JS_FALSE;

    /* Get the Java class of each element of the array */
    array_component_signature = signature->array_component_signature;
    component_class = array_component_signature->java_class;

    /* Create a new empty Java array with the same length as the JS array */
    java_array = (*jEnv)->CallStaticObjectMethod(jEnv, jlrArray, jlrArray_newInstance,
                                                 component_class, length);
    if (!java_array) {
        jsj_ReportJavaError(cx, jEnv, "Error while constructing empty array of %s",
                            jsj_GetJavaClassName(cx, jEnv, component_class));
        return JS_FALSE;
    }

    /* Convert each element of the JS array to an element of the Java array.
       If an error occurs, there is no need to worry about releasing the
       individual elements of the Java array - they will eventually be GC'ed
       by the JVM. */
    for (i = 0; i < length; i++) {
        if (!JS_LookupElement(cx, js_array, i, &js_val))
            return JS_FALSE;

        if (!jsj_SetJavaArrayElement(cx, jEnv, java_array, i, array_component_signature, js_val))
            return JS_FALSE;
    }

    /* Return the result array */
    *java_valuep = java_array;
    return JS_TRUE;
}