SeedObject sqlite_construct_database(SeedContext ctx, SeedObject constructor, size_t argument_count, const SeedValue arguments[], SeedException * exception) { SeedObject ret; gchar *file; sqlite3 *db; int rc; if (argument_count != 1) { seed_make_exception(ctx, exception, "ArgumentError", "sqlite.Database constructor expected 1 argument"); return (SeedObject) seed_make_null(ctx); } file = seed_value_to_string(ctx, arguments[0], exception); rc = sqlite3_open(file, &db); g_free(file); ret = seed_make_object(ctx, sqlite_class, db); seed_object_set_property(ctx, ret, "status", seed_value_from_int(ctx, rc, exception)); return ret; }
SeedValue seed_mpfr_remainder (SeedContext ctx, SeedObject function, SeedObject this_object, gsize argument_count, const SeedValue args[], SeedException *exception) { mpfr_rnd_t rnd; mpfr_ptr rop, op1, op2; gint ret; CHECK_ARG_COUNT("mpfr.remainder", 3); rop = seed_object_get_private(this_object); rnd = seed_value_to_mpfr_rnd_t(ctx, args[2], exception); if ( seed_value_is_object_of_class(ctx, args[0], mpfr_class) && seed_value_is_object_of_class(ctx, args[1], mpfr_class)) { op1 = seed_object_get_private(args[0]); op2 = seed_object_get_private(args[1]); } else { TYPE_EXCEPTION("mpfr.remainder", "mpfr_t"); } ret = mpfr_remainder(rop, op1, op2, rnd); return seed_value_from_int(ctx, ret, exception); }
SeedValue seed_mpfr_cmpabs (SeedContext ctx, SeedObject function, SeedObject this_object, gsize argument_count, const SeedValue args[], SeedException *exception) { mpfr_ptr rop, op; gint ret; CHECK_ARG_COUNT("mpfr.cmpabs", 1); rop = seed_object_get_private(this_object); if ( seed_value_is_object_of_class(ctx, args[0], mpfr_class) ) { op = seed_object_get_private(args[0]); } else { TYPE_EXCEPTION("mpfr.cmpabs", "mpfr_t"); } ret = mpfr_cmpabs(rop, op); return seed_value_from_int(ctx, ret, exception); }
SeedValue seed_mpfr_sub (SeedContext ctx, SeedObject function, SeedObject this_object, gsize argument_count, const SeedValue args[], SeedException *exception) { mpfr_rnd_t rnd; mpfr_ptr rop, op1, op2; gdouble dop1, dop2; gint ret; seed_mpfr_t argt1, argt2; /* only want 1 double argument. alternatively, could accept 2, add those, and set from the result*/ CHECK_ARG_COUNT("mpfr.sub", 3); rop = seed_object_get_private(this_object); rnd = seed_value_to_mpfr_rnd_t(ctx, args[2], exception); argt1 = seed_mpfr_arg_type(ctx, args[0], exception); argt2 = seed_mpfr_arg_type(ctx, args[1], exception); if ( (argt1 & argt2) == SEED_MPFR_MPFR ) { /* both mpfr_t */ op1 = seed_object_get_private(args[0]); op2 = seed_object_get_private(args[1]); ret = mpfr_sub(rop, op1, op2, rnd); } else if ( (argt1 | argt2) == (SEED_MPFR_MPFR | SEED_MPFR_DOUBLE) ) { /* a double and an mpfr_t. Figure out the order */ if ( argt1 == SEED_MPFR_MPFR ) { op1 = seed_object_get_private(args[0]); dop2 = seed_value_to_double(ctx, args[1], exception); mpfr_sub_d(rop, op1, dop2, rnd); } else { dop1 = seed_value_to_double(ctx, args[0], exception); op2 = seed_object_get_private(args[1]); mpfr_d_sub(rop, dop1, op2, rnd); } } else if ( (argt1 & argt2) == SEED_MPFR_DOUBLE ) { /* 2 doubles. hopefully doesn't happen */ dop1 = seed_value_to_double(ctx, args[0], exception); dop2 = seed_value_to_double(ctx, args[1], exception); ret = mpfr_set_d(rop, dop1 - dop2, rnd); } else { TYPE_EXCEPTION("mpfr.sub", "double or mpfr_t"); } return seed_value_from_int(ctx, ret, exception); }
static SeedValue value_from_ffi_type (SeedContext ctx, GType otype, GArgument *value, SeedException *exception) { switch (g_type_fundamental (otype)) { case G_TYPE_INT: return seed_value_from_int (ctx, value->v_int, exception); break; case G_TYPE_FLOAT: return seed_value_from_float (ctx, value->v_float, exception); break; case G_TYPE_DOUBLE: return seed_value_from_double (ctx, value->v_double, exception); break; case G_TYPE_BOOLEAN: return seed_value_from_boolean (ctx, value->v_boolean, exception); break; case G_TYPE_STRING: return seed_value_from_string (ctx, value->v_pointer, exception); break; case G_TYPE_CHAR: return seed_value_from_char (ctx, value->v_int, exception); break; case G_TYPE_UCHAR: return seed_value_from_uchar (ctx, value->v_uint, exception); break; case G_TYPE_UINT: return seed_value_from_uint (ctx, value->v_uint, exception); break; case G_TYPE_POINTER: return seed_make_pointer (ctx, value->v_pointer); break; case G_TYPE_LONG: return seed_value_from_long (ctx, value->v_long, exception); break; case G_TYPE_ULONG: return seed_value_from_ulong (ctx, value->v_ulong, exception); break; case G_TYPE_INT64: return seed_value_from_int64 (ctx, value->v_int64, exception); break; case G_TYPE_UINT64: return seed_value_from_uint64 (ctx, value->v_uint64, exception); break; case G_TYPE_NONE: return seed_make_null (ctx); default: g_warning ("Unsupported fundamental type in value_from_ffi_type: %s", g_type_name(g_type_fundamental (otype))); return seed_make_null (ctx); } }
SeedObject gjs_compat_define_gi_stuff(SeedEngine* eng) { SeedContext context = eng->context; SeedObject module; gboolean ret; module = seed_make_object(context, seed_create_class(&system_def), NULL); SeedValue seed = seed_object_get_property(context, eng->global, "Seed"); SeedValue argv = seed_object_get_property(context, seed, "argv"); ret = seed_object_set_property( context, module, "version", (SeedValue) seed_value_from_int(context, GJS_COMPAT_VERSION, NULL)); return module; }
SeedValue seed_sqlite_exec(SeedContext ctx, SeedObject function, SeedObject this_object, size_t argument_count, const SeedValue arguments[], SeedException * exception) { gchar *statement; gchar *sqlite_error = 0; sqlite3 *db; int rc; if (argument_count < 1) { seed_make_exception(ctx, exception, "ArgumentError", "sqlite.Database.exec expected 1 or 2 arguments"); return seed_make_null(ctx); } statement = seed_value_to_string(ctx, arguments[0], exception); db = seed_object_get_private(this_object); g_assert(db); rc = sqlite3_exec(db, statement, seed_sqlite_exec_callback, argument_count == 2 ? arguments[1] : 0, &sqlite_error); g_free(statement); if (rc != SQLITE_OK) { if (sqlite_error) { seed_make_exception(ctx, exception, "SqliteError", sqlite_error, NULL); sqlite3_free(sqlite_error); } return seed_make_null(ctx); } return seed_value_from_int(ctx, rc, exception); }
static SeedValue seed_rl_insert(SeedContext ctx, SeedObject function, SeedObject this_object, size_t argument_count, const SeedValue arguments[], SeedValue * exception) { gchar *ins; gint ret; CHECK_ARG_COUNT("readline.insert", 1); ins = seed_value_to_string (ctx, arguments[0], exception); ret = rl_insert_text (ins); g_free (ins); return seed_value_from_int (ctx, ret, exception); }
SeedValue seed_mpfr_sgn (SeedContext ctx, SeedObject function, SeedObject this_object, gsize argument_count, const SeedValue args[], SeedException *exception) { mpfr_ptr rop; gint ret; CHECK_ARG_COUNT("mpfr.sgn", 0); rop = seed_object_get_private(this_object); ret = mpfr_sgn(rop); return seed_value_from_int(ctx, ret, exception); }
SeedValue seed_pipe_write(SeedContext ctx, SeedObject function, SeedObject this_object, size_t argument_count, const SeedValue arguments[], SeedException * exception) { gchar *data; gsize written; gchar eol = '\n'; GET_CHANNEL; CHECK_ARG_COUNT("multiprocessing.pipe.write", 1); data = seed_value_to_string(ctx, arguments[0], exception); g_io_channel_write_chars(priv->write, data, -1, &written, 0); g_io_channel_write_chars(priv->write, &eol, 1, 0, 0); g_io_channel_flush(priv->write, 0); return seed_value_from_int(ctx, written, exception); }
SeedValue seed_mpfr_root (SeedContext ctx, SeedObject function, SeedObject this_object, gsize argument_count, const SeedValue args[], SeedException *exception) { mpfr_rnd_t rnd; mpfr_ptr rop, op; gint ret; gulong k; CHECK_ARG_COUNT("mpfr.root", 3); rop = seed_object_get_private(this_object); rnd = seed_value_to_mpfr_rnd_t(ctx, args[2], exception); if ( seed_value_is_object_of_class(ctx, args[0], mpfr_class) ) { op = seed_object_get_private(args[0]); } else { TYPE_EXCEPTION("mpfr.root", "mpfr_t"); } if ( seed_value_is_number(ctx, args[1]) ) { k = seed_value_to_ulong(ctx, args[1], exception); } else { TYPE_EXCEPTION("mpfr.root", "unsigned long int"); } ret = mpfr_root(rop, op, k, rnd); return seed_value_from_int(ctx, ret, exception); }
SeedValue seed_mpfr_prec_round (SeedContext ctx, SeedObject function, SeedObject this_object, gsize argument_count, const SeedValue args[], SeedException *exception) { mpfr_rnd_t rnd; mp_prec_t prec; mpfr_ptr rop; gint ret; CHECK_ARG_COUNT("mpfr.prec_round", 2); rop = seed_object_get_private(this_object); prec = seed_value_to_mpfr_prec_t(ctx, args[0], exception); rnd = seed_value_to_mpfr_rnd_t(ctx, args[1], exception); ret = mpfr_prec_round(rop, prec, rnd); return seed_value_from_int(ctx, ret, exception); }
SeedValue seed_mpfr_cmp_si_2exp (SeedContext ctx, SeedObject function, SeedObject this_object, gsize argument_count, const SeedValue args[], SeedException *exception) { mpfr_ptr op1; gulong op2; mp_exp_t exp; gint ret; CHECK_ARG_COUNT("mpfr.cmp_si_2exp", 2); op1 = seed_object_get_private(this_object); if ( seed_value_is_number(ctx, args[0]) ) { op2 = seed_value_to_ulong(ctx, args[0], exception); } else { TYPE_EXCEPTION("mpfr.cmp_si_2exp", "long int"); } if ( seed_value_is_number(ctx, args[1]) ) { exp = seed_value_to_mp_exp_t(ctx, args[1], exception); } else { TYPE_EXCEPTION("mpfr.cmp_si_2exp", "mp_exp_t"); } ret = mpfr_cmp_si_2exp(op1, op2, exp); return seed_value_from_int(ctx, ret, exception); }
void basic_types(TestSimpleFixture * fixture, gconstpointer _data) { // bool to/from JS equality gboolean bool_test_in = TRUE; SeedValue *bool_test = seed_value_from_boolean(fixture->context, bool_test_in, NULL); gboolean bool_test_out = seed_value_to_boolean(fixture->context, bool_test, NULL); g_assert(bool_test_in == bool_test_out); // uint to/from JS equality guint uint_test_in = 2946623; SeedValue *uint_test = seed_value_from_uint(fixture->context, uint_test_in, NULL); guint uint_test_out = seed_value_to_uint(fixture->context, uint_test, NULL); g_assert(uint_test_in == uint_test_out); // int to/from JS equality gint int_test_in = -54374; SeedValue *int_test = seed_value_from_int(fixture->context, int_test_in, NULL); gint int_test_out = seed_value_to_int(fixture->context, int_test, NULL); g_assert(int_test_in == int_test_out); // char to/from JS equality gchar char_test_in = -126; SeedValue *char_test = seed_value_from_char(fixture->context, char_test_in, NULL); gchar char_test_out = seed_value_to_char(fixture->context, char_test, NULL); g_assert(char_test_in == char_test_out); // uchar to/from JS equality guchar uchar_test_in = 250; SeedValue *uchar_test = seed_value_from_uchar(fixture->context, uchar_test_in, NULL); guchar uchar_test_out = seed_value_to_uchar(fixture->context, uchar_test, NULL); g_assert(uchar_test_in == uchar_test_out); // long to/from JS equality glong long_test_in = -454250; SeedValue *long_test = seed_value_from_long(fixture->context, long_test_in, NULL); glong long_test_out = seed_value_to_long(fixture->context, long_test, NULL); g_assert(long_test_in == long_test_out); // ulong to/from JS equality gulong ulong_test_in = 250; SeedValue *ulong_test = seed_value_from_ulong(fixture->context, ulong_test_in, NULL); gulong ulong_test_out = seed_value_to_ulong(fixture->context, ulong_test, NULL); g_assert(ulong_test_in == ulong_test_out); // int64 to/from JS equality gint64 int64_test_in = -54374; SeedValue *int64_test = seed_value_from_int64(fixture->context, int64_test_in, NULL); gint64 int64_test_out = seed_value_to_int64(fixture->context, int64_test, NULL); g_assert(int64_test_in == int64_test_out); // uint64 to/from JS equality guint64 uint64_test_in = 2946623; SeedValue *uint64_test = seed_value_from_uint64(fixture->context, uint64_test_in, NULL); guint64 uint64_test_out = seed_value_to_uint64(fixture->context, uint64_test, NULL); g_assert(uint64_test_in == uint64_test_out); // float to/from JS equality gfloat float_test_in = 1.618; SeedValue *float_test = seed_value_from_float(fixture->context, float_test_in, NULL); gfloat float_test_out = seed_value_to_float(fixture->context, float_test, NULL); g_assert(float_test_in == float_test_out); // double to/from JS equality gdouble double_test_in = 1.6134857638; SeedValue *double_test = seed_value_from_double(fixture->context, double_test_in, NULL); gdouble double_test_out = seed_value_to_double(fixture->context, double_test, NULL); g_assert(double_test_in == double_test_out); // string to/from JS equality gchar *string_test_in = "Hello, world!"; SeedValue *string_test = seed_value_from_string(fixture->context, string_test_in, NULL); gchar *string_test_out = seed_value_to_string(fixture->context, string_test, NULL); g_assert(strncmp(string_test_in, string_test_out, strlen(string_test_in)) == 0); // filename to/from JS equality gchar *filename_test_in = "/bin"; SeedValue *filename_test = seed_value_from_filename(fixture->context, filename_test_in, NULL); gchar *filename_test_out = seed_value_to_filename(fixture->context, filename_test, NULL); g_assert(strncmp(filename_test_in, filename_test_out, strlen(filename_test_in)) == 0); SeedValue si[2]; si[0] = seed_value_from_string (fixture->context, "Hi", NULL); si[1] = seed_value_from_int (fixture->context, 1, NULL); gint ni; gchar *ns; seed_value_to_format (fixture->context, "si", si, NULL, &ns, &ni, NULL); g_assert (ni == 1); g_assert (!strcmp(ns, "Hi")); }
/* returns an error message or NULL */ static DBusMessage * invoke_js_async_from_dbus(SeedContext ctx, DBusBusType bus_type, DBusMessage *method_call, SeedObject this_obj, SeedObject method_obj, SeedException *exception) { DBusMessage *reply; int argc; SeedValue *argv; DBusMessageIter arg_iter; GArray *values; SeedObject callback_object; SeedValue sender_string, signature_string; gboolean thrown; SeedValue ignored; const char *signature; reply = NULL; thrown = FALSE; argc = 0; argv = NULL; if (!seed_js_values_from_dbus(ctx, &arg_iter, &values, exception)) { if (!dbus_reply_from_exception(ctx, method_call, &reply, exception)) g_warning ("conversion of dbus method arg failed but no exception was set?"); return reply; } /* we will add an argument, the callback */ callback_object = seed_make_function(ctx, async_call_callback, "" /* anonymous */); g_assert(callback_object); g_array_append_val(values, callback_object); /* We attach the DBus sender and serial as properties to * callback, so we don't need to bother with memory managing them * if the callback is never called and just discarded.*/ sender_string = seed_value_from_string (ctx, dbus_message_get_sender (method_call), exception); if (!sender_string) { thrown = TRUE; goto out; } seed_object_set_property (ctx, callback_object, "_dbusSender", sender_string); seed_object_set_property (ctx, callback_object, "_dbusSerial", seed_value_from_int (ctx, dbus_message_get_serial (method_call), exception)); seed_object_set_property (ctx, callback_object, "_dbusBusType", seed_value_from_int (ctx, bus_type, exception)); if (!signature_from_method(ctx, method_obj, &signature, exception)) { thrown = TRUE; goto out; } signature_string = seed_value_from_string (ctx, signature, exception); if (!signature_string) { thrown = TRUE; goto out; } seed_object_set_property (ctx, callback_object, "_dbusOutSignature", signature_string); argc = values->len; argv = (SeedValue *)values->data; seed_object_call (ctx, method_obj, this_obj, argc, argv, &ignored); out: if (thrown) { if (!dbus_reply_from_exception(ctx, method_call, &reply, exception)) g_warning("conversion of dbus method arg failed but no exception was set?"); } g_array_free (values, TRUE); return reply; }
SeedValue seed_mpfr_pow (SeedContext ctx, SeedObject function, SeedObject this_object, gsize argument_count, const SeedValue args[], SeedException * exception) { mpfr_rnd_t rnd; mpfr_ptr rop, op1, op2; gint ret; glong iop; gulong uiop1, uiop2; seed_mpfr_t argt1, argt2; /* only want 1 double argument. alternatively, could accept 2, add those, and set from the result*/ CHECK_ARG_COUNT("mpfr.pow", 3); rop = seed_object_get_private(this_object); rnd = seed_value_to_mpfr_rnd_t(ctx, args[2], exception); argt1 = seed_mpfr_arg_type(ctx, args[0], exception); argt2 = seed_mpfr_arg_type(ctx, args[1], exception); if ( (argt1 & argt2) == SEED_MPFR_MPFR ) { /* both mpfr_t */ op1 = seed_object_get_private(args[0]); op2 = seed_object_get_private(args[1]); ret = mpfr_pow(rop, op1, op2, rnd); } else if ( (argt1 | argt2) == (SEED_MPFR_MPFR | SEED_MPFR_DOUBLE) ) { /* a double and an mpfr_t. Figure out the order */ /* FIXME: is this switching ui and si bad? si_pow doesn't exist, and it's all from double anyway */ if ( argt1 == SEED_MPFR_MPFR ) { op1 = seed_object_get_private(args[0]); iop = seed_value_to_long(ctx, args[1], exception); ret = mpfr_pow_si(rop, op1, iop, rnd); } else { uiop1 = seed_value_to_ulong(ctx, args[0], exception); op2 = seed_object_get_private(args[1]); ret = mpfr_ui_pow(rop, uiop1, op2, rnd); } } else if ( (argt1 & argt2) == SEED_MPFR_DOUBLE ) { /* pretend both ui */ uiop1 = seed_value_to_ulong(ctx, args[0], exception); uiop2 = seed_value_to_ulong(ctx, args[1], exception); ret = mpfr_ui_pow_ui(rop, uiop1, uiop2, rnd); } else { TYPE_EXCEPTION("mpfr.pow", "int or unsigned int and mpfr_t"); } return seed_value_from_int(ctx, ret, exception); }