Пример #1
0
static SEXP Julia_R_MD_NA_DataFrame(jl_value_t *Var)
{
  SEXP ans, names, rownames;
  char evalcmd[evalsize];
  int i;
  const char *dfname = "DataFrameName0tmp";
  jl_set_global(jl_main_module, jl_symbol(dfname), (jl_value_t *)Var);
  //Get Frame cols
  snprintf(evalcmd, evalsize, "size(%s,2)", dfname);
  jl_value_t *cols = jl_eval_string(evalcmd);
  int collen = jl_unbox_long(cols);
  jl_value_t *eachcolvector;
  jl_value_t *coltype;
  //Create VECSXP

  //Create SEXP for Each Column and assign
  PROTECT(ans = allocVector(VECSXP, collen));
  for (i = 0; i < collen; i++)
  {

    snprintf(evalcmd, evalsize, "%s[%d]", dfname, i + 1);
    eachcolvector = jl_eval_string(evalcmd);
    snprintf(evalcmd, evalsize, "isa(%s[%d],PooledDataArray)", dfname, i + 1);
    coltype = jl_eval_string(evalcmd);
    if (jl_unbox_bool(coltype))
      SET_VECTOR_ELT(ans, i, Julia_R_MD_NA_Factor(eachcolvector));
    else
      SET_VECTOR_ELT(ans, i, Julia_R_MD_NA(eachcolvector));
  }
  //set names attribute
  snprintf(evalcmd, evalsize, "names(%s)", dfname);
  jl_value_t *ret = jl_eval_string(evalcmd);
  jl_value_t *onesymbol;
  if (jl_is_array(ret))
  {
    PROTECT(names = allocVector(STRSXP, collen));
    for (i = 0; i < jl_array_len(ret); i++)
    {
      onesymbol = jl_arrayref((jl_array_t *)ret, i);
      if (jl_is_symbol(onesymbol))
        SET_STRING_ELT(names, i, mkChar(((jl_sym_t *)onesymbol)->name));
    }
    setAttrib(ans, R_NamesSymbol, names);
    UNPROTECT(1);
  }
  //set row names
  snprintf(evalcmd, evalsize, "size(%s,1)", dfname);
  jl_value_t *rows = jl_eval_string(evalcmd);
  int rowlen = jl_unbox_long(rows);
  PROTECT(rownames = allocVector(INTSXP, rowlen));
  for (i = 0; i < rowlen; i++)
    INTEGER(rownames)[i] = i + 1;
  setAttrib(ans, R_RowNamesSymbol, rownames);
  UNPROTECT(1);
  //set class as data frame
  setAttrib(ans, R_ClassSymbol, mkString("data.frame"));
  //SET_OBJECT(ans, 1) ;
  UNPROTECT(1);
  return ans;
}
Пример #2
0
// argument is the usr/lib directory where libjulia is, or NULL to guess.
// if that doesn't work, try the full path to the "lib" directory that
// contains lib/julia/sys.ji
DLLEXPORT void jl_init(char *julia_home_dir)
{
    libsupport_init();
    char *image_file = jl_locate_sysimg(julia_home_dir);
    julia_init(image_file);
    jl_set_const(jl_core_module, jl_symbol("JULIA_HOME"),
                 jl_cstr_to_string(julia_home));
    jl_module_export(jl_core_module, jl_symbol("JULIA_HOME"));
    jl_eval_string("Base.reinit_stdio()");
    jl_eval_string("Base.librandom_init()");
    jl_eval_string("Base.init_sched()");
    jl_eval_string("Base.init_head_sched()");
    jl_eval_string("Base.init_load_path()");
}
Пример #3
0
// argument is the usr/lib directory where libjulia is, or NULL to guess.
// if that doesn't work, try the full path to the "lib" directory that
// contains lib/julia/sys.ji
DLLEXPORT void jl_init(char *julia_home_dir)
{
    if (jl_is_initialized()) return;
    libsupport_init();
    char *image_file = jl_locate_sysimg(julia_home_dir, JL_SYSTEM_IMAGE_PATH);
    julia_init(image_file);
    jl_set_const(jl_core_module, jl_symbol("JULIA_HOME"),
                 jl_cstr_to_string(julia_home));
    jl_module_export(jl_core_module, jl_symbol("JULIA_HOME"));
    jl_eval_string("Base.reinit_stdio()");
    jl_eval_string("Base.Random.librandom_init()");
    jl_eval_string("Base.init_sched()");
    jl_eval_string("Base.init_head_sched()");
    jl_eval_string("Base.init_load_path()");
}
Пример #4
0
// First argument is the usr/lib directory where libjulia is, or NULL to guess.
// if that doesn't work, try the full path to the "lib" directory that
// contains lib/julia/sys.ji
// Second argument is the path of a system image file (*.ji) relative to the
// first argument path, or relative to the default julia home dir. The default
// is something like ../lib/julia/sys.ji
DLLEXPORT void jl_init_with_image(char *julia_home_dir, char *image_relative_path)
{
    if (jl_is_initialized()) return;
    libsupport_init();
    if (image_relative_path == NULL)
        image_relative_path = JL_SYSTEM_IMAGE_PATH;
    char *image_file = jl_locate_sysimg(julia_home_dir, image_relative_path);
    julia_init(image_file);
    jl_set_const(jl_core_module, jl_symbol("JULIA_HOME"),
                 jl_cstr_to_string(julia_home));
    jl_module_export(jl_core_module, jl_symbol("JULIA_HOME"));
    jl_eval_string("Base.early_init()");
    jl_eval_string("Base.init_head_sched()");
    jl_eval_string("Base.init_load_path()");
    jl_exception_clear();
}
Пример #5
0
// First argument is the usr/lib directory where libjulia is, or NULL to guess.
// if that doesn't work, try the full path to the "lib" directory that
// contains lib/julia/sys.ji
// Second argument is the path of a system image file (*.ji) relative to the
// first argument path, or relative to the default julia home dir. The default
// is something like ../lib/julia/sys.ji
DLLEXPORT void jl_init_with_image(const char *julia_home_dir, const char *image_relative_path)
{
    if (jl_is_initialized()) return;
    libsupport_init();
    jl_compileropts.julia_home = julia_home_dir;
    if (image_relative_path != NULL)
        jl_compileropts.image_file = image_relative_path;
    julia_init(JL_IMAGE_JULIA_HOME);
    //TODO: these should be part of Multi.__init__()
    //currently, we have them here since we may not want them
    //getting unconditionally set from Base.__init__()
    jl_eval_string("Base.init_parallel()");
    jl_eval_string("Base.init_bind_addr(ARGS)");
    jl_eval_string("Base.init_head_sched()");
    jl_exception_clear();
}
Пример #6
0
int square(double a[],double b[])
{
    
    // Init Julia
    jl_init_with_image("/Users/fgans/julia/julia-4d1b751dda/lib/julia", "sys.ji");
    JL_SET_STACK_BASE;
    
    // Define Array Type for 1D Array
    jl_value_t* array_type = jl_apply_array_type(jl_float64_type, 1);
    
    // Connect C-Arrays to julia Arrays
    jl_array_t *a_jl = jl_ptr_to_array_1d(array_type, a, 1, 0);
    jl_array_t *b_jl = jl_ptr_to_array_1d(array_type, b, 1, 0);
   

    // Load julia code
    jl_eval_string("include(\"square.jl\")");
    
    // Get function
    jl_function_t *func  = jl_get_function(jl_main_module, "square");
    if (func==NULL) {
        printf("Function not found!\n");
        return -1;
    }
    
    // Apply function
    jl_call2(func, (jl_value_t*)a_jl,(jl_value_t*)b_jl);
    if (jl_exception_occurred()) printf("%s \n", jl_typeof_str(jl_exception_occurred()));
    
    

    return 0;
}
Пример #7
0
DLLEXPORT  const char *jlapi_get_stdout() {
  jl_value_t *out;
  
  if(strcmp(jlapi_mode,"rcqls")==0) {
    out=jl_eval_string("seek(STDOUT, 0);jl4rb_out = takebuf_string(STDOUT);truncate(STDOUT, 0);jl4rb_out");
    return jl_bytestring_ptr(out);
  } else return "";
}
Пример #8
0
//basically factor in R is 1-dim INTSXP and contain levels
static jl_value_t *TransArrayToPoolDataArray(jl_array_t *mArray, jl_array_t *mpoolArray, size_t len, const char *VarName)
{
  char evalcmd[evalsize];
  jl_set_global(jl_main_module, jl_symbol("varpools"), (jl_value_t *)mpoolArray);
  jl_set_global(jl_main_module, jl_symbol("varrefs"), (jl_value_t *)mArray);
  snprintf(evalcmd, evalsize, "%s=PooledDataArray(ASCIIString,Uint32,%d)", VarName, len);
  jl_eval_string(evalcmd);
  snprintf(evalcmd, evalsize, "%s.pool=%s", VarName, "varpools");
  jl_eval_string(evalcmd);
  snprintf(evalcmd, evalsize, "%s.refs=%s", VarName, "varrefs");
  jl_eval_string(evalcmd);
  jl_value_t *ret = jl_eval_string((char *)VarName);
  if (jl_exception_occurred())
  {
    jl_show(jl_stderr_obj(), jl_exception_occurred());
    Rprintf("\n");
    jl_exception_clear();
    return (jl_value_t *) jl_nothing;
  }
  return ret;
}
Пример #9
0
//try load julia DataArrays and DataFrames packages
SEXP Julia_LoadDataArrayFrame()
{
  jl_eval_string("using DataArrays,DataFrames");
  if (jl_exception_occurred())
  {
    jl_show(jl_stderr_obj(), jl_exception_occurred());
    Rprintf("\n");
    jl_exception_clear();
  }
  else
    DataArrayFrameInited = 1;
  return R_NilValue;
}
Пример #10
0
static SEXP Julia_R_MD_NA_Factor(jl_value_t *Var)
{
  SEXP ans = R_NilValue;
  char *strData = "Varname0tmp.refs";
  char *strlevels = "VarPools=Array(ASCIIString,length(Varname0tmp.pool))\r\n"
                    "for i in 1:length(Varname0tmp.pool)\r\n"
                    "VarPools[i]=string(Varname0tmp.pool[i])\r\n"
                    "end\r\n"
                    "VarPools\r\n";
  jl_set_global(jl_main_module, jl_symbol("Varname0tmp"), (jl_value_t *)Var);
  jl_value_t *retData = jl_eval_string(strData);
  jl_value_t *retlevels = jl_eval_string(strlevels);
  //first get refs data,dims=n
  //caution this convert to int32 SEXP,it should be ok in reality,
  //but if have a lot factor may be cause int32 overrun.
  ans = Julia_R_MD_INT(retData);
  PROTECT(ans);
  //second setAttrib R levels and class
  SEXP levels = Julia_R_MD(retlevels);
  setAttrib(ans, R_LevelsSymbol, levels);
  setAttrib(ans, R_ClassSymbol, mkString("factor"));
  UNPROTECT(1);
  return ans;
}
Пример #11
0
//first pass creat array then convert it to DataArray
//second pass assign NA to element
static jl_value_t *TransArrayToDataArray(jl_array_t *mArray, jl_array_t *mboolArray, const char *VarName)
{ 
  char evalcmd[evalsize];
  jl_set_global(jl_main_module, jl_symbol("TransVarName"), (jl_value_t *)mArray);
  jl_set_global(jl_main_module, jl_symbol("TransVarNamebool"), (jl_value_t *)mboolArray);
  snprintf(evalcmd, evalsize, "%s=DataArray(TransVarName,TransVarNamebool)", VarName);
  jl_value_t *ret = jl_eval_string(evalcmd);
  if (jl_exception_occurred())
  {
    jl_show(jl_stderr_obj(), jl_exception_occurred());
    Rprintf("\n");
    jl_exception_clear();
    return (jl_value_t *) jl_nothing;
  }
  return ret;
}
Пример #12
0
static jl_value_t *R_Julia_MD_NA_DataFrame(SEXP Var, const char *VarName)
{
  SEXP names = getAttrib(Var, R_NamesSymbol);
  size_t len = LENGTH(Var);
  if (TYPEOF(Var) != VECSXP || len == 0 || names == R_NilValue)
    return (jl_value_t *) jl_nothing;
  char evalcmd[evalsize];
  char eltcmd[eltsize];
  const char *onename;
  SEXP elt;
  for (size_t i = 0; i < len; i++)
  {
    snprintf(eltcmd, eltsize, "%sdfelt%d", VarName, i + 1);
    elt = VECTOR_ELT(Var, i);
    //vector is factor or not
    if (getAttrib(elt, R_LevelsSymbol) != R_NilValue)
      R_Julia_MD_NA_Factor(elt, eltcmd);
    else
      R_Julia_MD_NA(elt, eltcmd);

    onename = CHAR(STRING_ELT(names, i));
    if (i == 0)
      snprintf(evalcmd, evalsize, "%s=DataFrame(%s =%s)", VarName, onename, eltcmd);
    else
      snprintf(evalcmd, evalsize, "%s[symbol(\"%s\")]=%s", VarName, onename, eltcmd);
    //Rprintf("%s\n",evalcmd);
    jl_eval_string(evalcmd);
    if (jl_exception_occurred())
    {
      jl_show(jl_stderr_obj(), jl_exception_occurred());
      Rprintf("\n");
      jl_exception_clear();
      return (jl_value_t *) jl_nothing;
    }
  }
  return (jl_value_t *) jl_nothing;;
}
Пример #13
0
void SqwJl::SetVars(const std::vector<SqwBase::t_var>& vecVars)
{
	if(!m_bOk)
	{
		tl::log_err("Julia interpreter has not initialised, cannot set variables.");
		return;
	}

	std::ostringstream ostrEval;
	for(const SqwBase::t_var& var : vecVars)
	{
		const std::string& strName = std::get<0>(var);
		const std::string& strType = std::get<1>(var);
		const std::string& strValue = std::get<2>(var);

		if(!strName.length()) continue;

		ostrEval << strName << " = ";

		if(strType.length())
		{
			// if a type is given, filter out some names
			if(strType[0] == '#' || strType == "Module")
				continue;

			// with cast
			ostrEval << strType << "(" << strValue << ");\n";
		}
		else
		{
			//without cast
			ostrEval << strValue << ";\n";
		}
	}
	jl_eval_string(ostrEval.str().c_str());
}
Пример #14
0
static NOINLINE int true_main(int argc, char *argv[])
{
    if (jl_core_module != NULL) {
        jl_array_t *args = (jl_array_t*)jl_get_global(jl_core_module, jl_symbol("ARGS"));
        if (args == NULL) {
            args = jl_alloc_cell_1d(0);
            JL_GC_PUSH1(&args);
            jl_set_const(jl_core_module, jl_symbol("ARGS"), (jl_value_t*)args);
            JL_GC_POP();
        }
        assert(jl_array_len(args) == 0);
        jl_array_grow_end(args, argc);
        int i;
        for (i=0; i < argc; i++) {
            jl_value_t *s = (jl_value_t*)jl_cstr_to_string(argv[i]);
            jl_set_typeof(s,jl_utf8_string_type);
            jl_arrayset(args, s, i);
        }
    }

    jl_function_t *start_client = jl_base_module ?
        (jl_function_t*)jl_get_global(jl_base_module, jl_symbol("_start")) : NULL;

    if (start_client) {
        jl_apply(&start_client, 1);
        return 0;
    }

    // run program if specified, otherwise enter REPL
    if (argc > 0) {
        if (strcmp(argv[0], "-")) {
            return exec_program(argv[0]);
        }
    }

    ios_puts("WARNING: Base._start not defined, falling back to economy mode repl.\n", ios_stdout);
    if (!jl_errorexception_type)
        ios_puts("WARNING: jl_errorexception_type not defined; any errors will be fatal.\n", ios_stdout);

    while (!ios_eof(ios_stdin)) {
        char *volatile line = NULL;
        JL_TRY {
            ios_puts("\njulia> ", ios_stdout);
            ios_flush(ios_stdout);
            line = ios_readline(ios_stdin);
            jl_value_t *val = (jl_value_t*)jl_eval_string(line);
            if (jl_exception_occurred()) {
                jl_printf(JL_STDERR, "error during run:\n");
                jl_static_show(JL_STDERR, jl_exception_in_transit);
                jl_exception_clear();
            }
            else if (val) {
                jl_static_show(JL_STDOUT, val);
            }
            jl_printf(JL_STDOUT, "\n");
            free(line);
            line = NULL;
            uv_run(jl_global_event_loop(),UV_RUN_NOWAIT);
        }
        JL_CATCH {
            if (line) {
                free(line);
                line = NULL;
            }
            jl_printf(JL_STDERR, "\nparser error:\n");
            jl_static_show(JL_STDERR, jl_exception_in_transit);
            jl_printf(JL_STDERR, "\n");
            jlbacktrace();
        }
    }
    return 0;
}
Пример #15
0
void td_jl_eval(td_val_t *out, char *str)
{
    jl_value_t *v = jl_eval_string(str);
    to_td_val(out, v);
}
Пример #16
0
static SEXP Julia_R_MD_NA(jl_value_t *Var)
{
  SEXP ans = R_NilValue;
  char *strData = "Varname0tmp.data";
  char *strNA = "bitunpack(Varname0tmp.na)";
  jl_set_global(jl_main_module, jl_symbol("Varname0tmp"), (jl_value_t *)Var);
  jl_value_t *retData = jl_eval_string(strData);
  jl_value_t *retNA = jl_eval_string(strNA);
  jl_value_t *val;

  if (((jl_array_t *)retData)->ptrarray)
    val = jl_cellref(retData, 0);
  else
    val = jl_arrayref((jl_array_t *)retData, 0);
  int len = jl_array_len(retData);
  if (len == 0)
    return ans;

  int ndims = jl_array_ndims(retData);
  SEXP dims;
  PROTECT(dims = allocVector(INTSXP, ndims));
  for (size_t i = 0; i < ndims; i++)
    INTEGER(dims)[i] = jl_array_dim(retData, i);
  UNPROTECT(1);

  //bool array
  char *pNA = (char *) jl_array_data(retNA);

  if (jl_is_bool(val))
  {
    char *p = (char *) jl_array_data(retData);
    PROTECT(ans = allocArray(LGLSXP, dims));
    for (size_t i = 0; i < len; i++)
      if (pNA[i])
        LOGICAL(ans)[i] = NA_LOGICAL;
      else
        LOGICAL(ans)[i] = p[i];
    UNPROTECT(1);
  }
  else if (jl_is_int32(val))
  {
    int32_t *p = (int32_t *) jl_array_data(retData);
    jlint_to_r_na;
  }
  //int64
  else if (jl_is_int64(val))
  {
    int64_t *p = (int64_t *) jl_array_data(retData);
    jlbiggerint_to_r_na;
  }
  //more integer type
  else if (jl_is_int8(val))
  {
    int8_t *p = (int8_t *) jl_array_data(retData);
    jlint_to_r_na;
  }
  else if (jl_is_int16(val))
  {
    int16_t *p = (int16_t *) jl_array_data(retData);
    jlint_to_r_na;
  }
  else if (jl_is_uint8(val))
  {
    uint8_t *p = (uint8_t *) jl_array_data(retData);
    jlint_to_r_na;
  }
  else if (jl_is_uint16(val))
  {
    uint16_t *p = (uint16_t *) jl_array_data(retData);
    jlint_to_r_na;
  }
  else if (jl_is_uint32(val))
  {
    uint32_t *p = (uint32_t *) jl_array_data(retData);
    jlbiggerint_to_r_na;
  }
  else if (jl_is_uint64(val))
  {
    uint64_t *p = (uint64_t *) jl_array_data(retData);
    jlbiggerint_to_r_na;
  }
  //double
  else if (jl_is_float64(val))
  {
    double *p = (double *) jl_array_data(retData);
    jlfloat_to_r_na;
  }
  else if (jl_is_float32(val))
  {
    float *p = (float *) jl_array_data(retData);
    jlfloat_to_r_na;
  }
  //convert string array to STRSXP
  else if (jl_is_utf8_string(val))
  {
    PROTECT(ans = allocArray(STRSXP, dims));
    for (size_t i = 0; i < len; i++)
      if (pNA[i])
        SET_STRING_ELT(ans, i, NA_STRING);
      else
        SET_STRING_ELT(ans, i, mkCharCE(jl_string_data(jl_cellref(retData, i)), CE_UTF8));
    UNPROTECT(1);
  }
  else if (jl_is_ascii_string(val))
  {
    PROTECT(ans = allocArray(STRSXP, dims));
    for (size_t i = 0; i < len; i++)
      if (pNA[i])
        SET_STRING_ELT(ans, i, NA_STRING);
      else
        SET_STRING_ELT(ans, i, mkChar(jl_string_data(jl_cellref(retData, i))));
    UNPROTECT(1);
  }
  return ans;
}
Пример #17
0
//-|  mode=="rcqls" is for rcqls development, mode="tty" is for initialization of STDOUT, STDERR with C API)
//-|  other value of mode mean standard jlapi.c
DLLEXPORT void jlapi_init(char *julia_home_dir, char* mode) {
  libsupport_init();
  char *image_file = jl_locate_sysimg(julia_home_dir);
  printf("image-file=%s\n",image_file);
  julia_init(image_file);
  jlapi_mode=mode;

  jl_set_const(jl_core_module, jl_symbol("JULIA_HOME"),
               jl_cstr_to_string(julia_home));
  jl_module_export(jl_core_module, jl_symbol("JULIA_HOME"));
  //-| This avoid LD_PRELOAD on linux since shared objects not exported
  //-| Maybe fix this in a better way with options compilation.
  char julia_api_libname[512];
#if defined(_OS_WINDOWS_)
  const char *shlib_ext=".dll";
  const char *sep="\\";
#elif defined(__APPLE__)
  const char *shlib_ext=".dylib";
  const char *sep="/";
#else
  const char *shlib_ext=".so";
  const char *sep="/";
#endif

  snprintf(julia_api_libname, sizeof(julia_api_libname), "%s%s%s%s%s%s",
          julia_home_dir, sep,"julia",sep,"libjulia-api",shlib_ext);
  load_library_permanently(julia_api_libname);
  if(strcmp(mode,"rcqls")<=0) { // cqls, rcqls
    //-| Called first to fix the DL_LOAD_PATH needed to (dl)open library (libpcre for example)
    //-| Replacement of Base.init_load_path()
    //-| Update 01/08/2013: No need to set DL_LOAD_PATH, just push 
    //-| jl_set_global(jl_base_module,jl_symbol("DL_LOAD_PATH"),jl_eval_string("ByteString[join([JULIA_HOME,\"..\",\"lib\",\"julia\"],Base.path_separator)]"));
    jl_eval_string("Base.push!(DL_LOAD_PATH,join([JULIA_HOME,\"..\",\"lib\",\"julia\"],Base.path_separator))");
    //-| DL_LOAD_PATH is a global constant already defined before and then not overloaded by julia
    //-| Only LOAD_PATH would be initialized (needs libpcre because of abspath)!
    jl_eval_string("vers = \"v$(VERSION.major).$(VERSION.minor)\"");
    jl_set_global(jl_base_module,jl_symbol("LOAD_PATH"),jl_eval_string("ByteString[abspath(JULIA_HOME,\"..\",\"local\",\"share\",\"julia\",\"site\",vers),abspath(JULIA_HOME,\"..\",\"share\",\"julia\",\"site\",vers)]")); 
  } else jl_eval_string("Base.init_load_path()");
  if(strcmp(mode,"tty")==0) {
    jl_eval_string("Base.reinit_stdio()");
    jl_set_global(jl_base_module,jl_symbol("STDIN"),jl_eval_string("Base.init_stdio(ccall(:jl_stdin_stream ,Ptr{Void},()),0)"));
    //-| 2 next lines fails even it is if no more necessary
    //-| Update 27/07/13: no more crash but stuck when print.
    jl_set_global(jl_base_module,jl_symbol("STDOUT"),jl_eval_string("Base.init_stdio(ccall(:jl_stdout_stream,Ptr{Void},()),1)"));
    jl_set_global(jl_base_module,jl_symbol("STDERR"),jl_eval_string("Base.init_stdio(ccall(:jl_stderr_stream,Ptr{Void},()),2)"));
  } else if(strcmp(mode,"rcqls")<=0) { //cqls, rcqls
    jl_eval_string("Base.reinit_stdio()");
    //-| STDIN, STDOUT and STDERR not properly loaded
    //-| I prefer redirection of STDOUT and STDERR in IOBuffer (maybe STDIN ???)
      jl_set_global(jl_base_module,jl_symbol("STDIN"),jl_eval_string("Base.init_stdio(ccall(:jl_stdin_stream ,Ptr{Void},()),0)"));
      //jl_set_global(jl_base_module,jl_symbol("STDIN"),jl_eval_string("IOBuffer()"));
      jl_set_global(jl_base_module,jl_symbol("STDOUT"),jl_eval_string("IOBuffer()"));
      jl_set_global(jl_base_module,jl_symbol("STDERR"),jl_eval_string("IOBuffer()"));
  } else jl_eval_string("Base.reinit_stdio()");
  jl_eval_string("Base.fdwatcher_reinit()");
  jl_eval_string("Base.Random.librandom_init()");
  jl_eval_string("Base.check_blas()");
  jl_eval_string("LinAlg.init()");
  jl_eval_string("Sys.init()");
  jl_eval_string("Base.init_sched()");
  jl_eval_string("Base.init_head_sched()");
  jl_eval_string("Base.try_include(abspath(ENV[\"HOME\"],\".juliarc.jl\"))");
  if(strcmp(mode,"rcqls")==0) { 
    jl_eval_string("println(\"Julia initialized!\")");
    jlapi_print_stdout();
  }
}
Пример #18
0
int main()
{
    jl_init(NULL);

    {
        // Simple running Julia code

        jl_eval_string("println(sqrt(2.0))");
    }

    {
        // Accessing the return value

        jl_value_t *ret = jl_eval_string("sqrt(2.0)");

        if (jl_is_float64(ret)) {
            double retDouble = jl_unbox_float64(ret);
            printf("sqrt(2.0) in C: %e\n", retDouble);
        }
    }

    {
        // Same as above but with function handle (more flexible)

        jl_function_t *func = jl_get_function(jl_base_module, "sqrt");
        jl_value_t* argument = jl_box_float64(2.0);
        jl_value_t* ret = jl_call1(func, argument);

        if (jl_is_float64(ret)) {
            double retDouble = jl_unbox_float64(ret);
            printf("sqrt(2.0) in C: %e\n", retDouble);
        }
    }

    {
        // 1D arrays

        jl_value_t* array_type = jl_apply_array_type( jl_float64_type, 1 );
        jl_array_t* x          = jl_alloc_array_1d(array_type , 10);
        JL_GC_PUSH1(&x);

        double* xData = jl_array_data(x);

        size_t i;
        for(i=0; i<jl_array_len(x); i++)
            xData[i] = i;

        jl_function_t *func  = jl_get_function(jl_base_module, "reverse!");
        jl_call1(func, (jl_value_t*) x);

        printf("x = [");
        for(i=0; i<jl_array_len(x); i++)
            printf("%e ", xData[i]);
        printf("]\n");

        JL_GC_POP();
    }

    {
        // define julia function and call it

        jl_eval_string("my_func(x) = 2*x");

        jl_function_t *func = jl_get_function(jl_current_module, "my_func");
        jl_value_t* arg = jl_box_float64(5.0);
        double ret = jl_unbox_float64(jl_call1(func, arg));

        printf("my_func(5.0) = %f\n", ret);
    }

    {
        // call c function

        jl_eval_string("println( ccall( :my_c_sqrt, Float64, (Float64,), 2.0 ) )");
    }

    {
        // check for exceptions

        jl_eval_string("this_function_does_not_exist()");

        if (jl_exception_occurred()) {
            jl_show(jl_stderr_obj(), jl_exception_occurred());
            jl_printf(jl_stderr_stream(), "\n");
        }
    }

    jl_atexit_hook();
    return 0;
}