Add printerr
authorMatt Arsenault <arsenm2@rpi.edu>
Wed, 6 Jan 2010 17:34:59 +0000 (12:34 -0500)
committerTim Horton <hortont424@gmail.com>
Fri, 22 Jan 2010 01:34:30 +0000 (20:34 -0500)
libseed/seed-builtins.c
libseed/seed-builtins.h
libseed/seed-engine.c

index 5821ad0..58b1159 100644 (file)
@@ -24,6 +24,7 @@
 #include <signal.h>
 
 JSValueRef seed_print_ref;
+JSValueRef seed_printerr_ref;
 
 static JSValueRef
 seed_include (JSContextRef ctx,
@@ -230,6 +231,31 @@ seed_print (JSContextRef ctx,
   return JSValueMakeUndefined (ctx);
 }
 
+static JSValueRef
+seed_printerr (JSContextRef ctx,
+              JSObjectRef function,
+              JSObjectRef this_object,
+              size_t argumentCount,
+              const JSValueRef arguments[],
+              JSValueRef *exception)
+{
+  gchar *buf;
+  if (argumentCount != 1)
+    {
+      seed_make_exception (ctx, exception, "ArgumentError",
+                          "printerr expected 1 argument, got %zd",
+                          argumentCount);
+      return JSValueMakeNull (ctx);
+    }
+
+  buf = seed_value_to_string (ctx, arguments[0], exception);
+
+  g_printerr ("%s\n", buf);
+  g_free (buf);
+
+  return JSValueMakeUndefined (ctx);
+}
+
 const gchar *
 seed_g_type_name_to_string (GITypeInfo * type)
 {
@@ -487,6 +513,13 @@ seed_init_builtins (SeedEngine * local_eng, gint * argc, gchar *** argv)
                            seed_print_ref);
   JSValueProtect (local_eng->context, seed_print_ref);
 
+  seed_printerr_ref =
+    JSObjectMakeFunctionWithCallback (local_eng->context, NULL, &seed_printerr);
+  seed_object_set_property (local_eng->context, obj, "printerr", seed_printerr_ref);
+  seed_object_set_property (local_eng->context, local_eng->global, "printerr",
+                           seed_printerr_ref);
+  JSValueProtect (local_eng->context, seed_printerr_ref);
+
   seed_create_function (local_eng->context,
                        "check_syntax", &seed_check_syntax, obj);
   seed_create_function (local_eng->context,
index f27462c..09285b1 100644 (file)
@@ -23,6 +23,7 @@
 #include "seed-private.h"
 
 extern JSValueRef seed_print_ref;
+extern JSValueRef seed_printerr_ref;
 
 void seed_init_builtins (SeedEngine * local_eng, gint * argc, gchar *** argv);
 
index 6e02230..1f0e7ae 100644 (file)
@@ -69,7 +69,7 @@ static const GDebugKey seed_debug_keys[] = {
 };
 #endif /* SEED_ENABLE_DEBUG */
 
-static bool 
+static bool
 seed_gobject_has_instance (JSContextRef ctx, JSObjectRef constructor,
                          JSValueRef possible_instance, JSValueRef* exception)
 {
@@ -78,7 +78,7 @@ seed_gobject_has_instance (JSContextRef ctx, JSObjectRef constructor,
       !JSValueIsObject (ctx, possible_instance) ||
       !JSValueIsObjectOfClass (ctx, possible_instance, gobject_class))
     return FALSE;
-  
+
   constructor_type = (GType) JSObjectGetPrivate (constructor);
   value_type = G_OBJECT_TYPE ((GObject *)
                              JSObjectGetPrivate ((JSObjectRef) possible_instance));
@@ -104,6 +104,7 @@ seed_prepare_global_context (JSContextRef ctx)
   seed_object_set_property (ctx, global, "GType", seed_gtype_constructor);
   seed_object_set_property (ctx, global, "Seed", seed_obj_ref);
   seed_object_set_property (ctx, global, "print", seed_print_ref);
+  seed_object_set_property (ctx, global, "printerr", seed_printerr_ref);
 
 
   JSEvaluateScript (ctx, defaults_script, NULL, NULL, 0, NULL);
@@ -1343,7 +1344,7 @@ seed_engine_destroy (SeedEngine *eng)
   JSValueUnprotect (eng->context, eng->global);
   JSGlobalContextRelease (eng->context);
   JSContextGroupRelease (eng->group);
-  
+
   g_free (eng);
 }
 
@@ -1375,7 +1376,7 @@ seed_init_with_context_group (gint * argc,
       SEED_NOTE (MISC, "failed to parse arguments.");
       return FALSE;
     }
-  
+
   if (seed_arg_print_version)
     {
       g_print("%s\n", "Seed " VERSION);
@@ -1469,3 +1470,4 @@ seed_init (gint * argc, gchar *** argv)
 
   return seed_init_with_context_group (argc, argv, context_group);
 }
+