1. Armin Ronacher
  2. arana-main

Commits

mitsuhiko  committed a70aab1

The interpreter now has an array of special singletons so that they are not shared (currently out of memory and the empty tuple)

  • Participants
  • Parent commits 597ba11
  • Branches default

Comments (0)

Files changed (4)

File arana/error.c

View file
 void
 arana_out_of_memory(AR_SIG)
 {
-    AR_RAISE(AI->_out_of_memory_error);
+    AR_RAISE(AR_SINGLETON(OUT_OF_MEMORY));
 }
 
 AR

File arana/interpreter.c

View file
     if (!(AI = AR_ALLOCT(struct _ArInterpreter)))
         AR_FATAL("Could not initialize interpreter.\n");
 
-    /* create the out of memory object for AR_RAISE_OOM */
-    AI->_out_of_memory_error = AR_MEMORY_ERROR("Out of Memory");
+    /* create the special singletons */
+    arana_initialize_singletons(AR_ISIG);
 
     /* create modules and type map */
     AI->modules = AR_STRINGMAP();
 
 }
 
+/* creates all the special singletons such as the out of memory error
+   or the empty tuple. */
+void
+arana_initialize_singletons(AR_SIG)
+{
+    AR_SINGLETON(OUT_OF_MEMORY) = AR_MEMORY_ERROR("Out of Memory");
+
+    /* null out the empty tuple so that AR_TUPLE(0) does not try
+       to use the not yet created one. */
+    AR_SINGLETON(EMPTY_TUPLE) = AR_NULL;
+    AR_SINGLETON(EMPTY_TUPLE) = AR_TUPLE(0);
+}
+
 /* creates the builtin module with all the types in */
 AR
 arana_initialize_builtins(AR_SIG)

File arana/interpreter.h

View file
 #define AR_FRAME(code) arana_new_frame(AR_ISIG, code)
 #define AR_SET_LOCAL(f, k, v) AR_STRINGMAP_SET((f)->locals, k, v)
 
+/* special singletons */
+#define AR_SINGLETON_COUNT 2
+#define AR_OUT_OF_MEMORY_SINGLETON 0
+#define AR_EMPTY_TUPLE_SINGLETON 1
+#define AR_SINGLETON(x) AI->singletons[AR_##x##_SINGLETON]
+
 struct _ArInterpreter {
     AR modules;
     AR error;
     struct _ArTraceback *traceback;
     jmp_buf _error_env;
     int _error_managed;
-    AR _out_of_memory_error;
+    AR singletons[AR_SINGLETON_COUNT];
     kh_ArTypeMap_t *type_map;
 };
 
 struct _ArFrame **f_out);
 AR_API_FUNC(ArFrame *) arana_new_frame(AR_SIG, struct _ArCode *code);
 
+AR_API_FUNC(void) arana_initialize_singletons(AR_SIG);
 AR_API_FUNC(AR) arana_initialize_builtins(AR_SIG);
 AR_API_FUNC(AR) arana_module_new(AR_SIG, char *name);
 AR_API_FUNC(void) arana_module_register(AR_SIG, AR self);

File arana/tuple.c

View file
 AR
 arana_tuple_new(AR_SIG, Ar_size_t size)
 {
-    static AR empty_tuple = AR_NULL;
-    if (size == 0 && empty_tuple != AR_NULL)
-        return empty_tuple;
+    if (size == 0 && AR_SINGLETON(EMPTY_TUPLE))
+        return AR_SINGLETON(EMPTY_TUPLE);
     ArTuple *self = (ArTuple *)AR_ALLOCTE(ArTuple, sizeof(AR) * size);
     if (!self)
         AR_RAISE_OOM();
     AR_OBJECT_INIT(self, ArTupleType);
     self->size = size;
-    if (size == 0)
-        empty_tuple = (AR)self;
     return (AR)self;
 }