Permalink
Browse files

Refactor reflect::TypeOf

  • Loading branch information...
1 parent 9ec3190 commit 4b7ffc0606d92e9a0f038c2272f52d0f3a178c69 @JukkaL committed Sep 25, 2011
Showing with 43 additions and 35 deletions.
  1. +3 −3 Makefile.depend
  2. +1 −32 src/reflect_module.c
  3. +37 −0 src/runtime.c
  4. +2 −0 src/runtime.h
View
6 Makefile.depend
@@ -254,7 +254,7 @@ src/io_text.o: src/io_text.c src/alore.h src/value.h src/common.h \
src/encodings_module.o: src/encodings_module.c src/alore.h src/value.h \
src/common.h src/aconfig.h config.h src/module.h src/thread.h \
src/globals.h src/errmsg.h src/encodings_module.h src/str.h src/mem.h \
- src/gc.h src/heapalloc.h src/debug_params.h
+ src/gc.h src/heapalloc.h src/debug_params.h src/util.h
src/encodings_tables.o: src/encodings_tables.c src/encodings_module.h
src/errno_module.o: src/errno_module.c src/aconfig.h config.h src/alore.h \
src/value.h src/common.h src/module.h src/thread.h src/globals.h \
@@ -285,8 +285,8 @@ src/athread_win32.o: src/athread_win32.c src/athread.h src/aconfig.h config.h
src/reflect_module.o: src/reflect_module.c src/alore.h src/value.h \
src/common.h src/aconfig.h config.h src/module.h src/thread.h \
src/globals.h src/errmsg.h src/runtime.h src/operator.h src/memberid.h \
- src/class.h src/symtable.h src/std_module.h src/str.h src/mem.h \
- src/internal.h src/gc.h src/heapalloc.h src/debug_params.h
+ src/class.h src/symtable.h src/str.h src/mem.h src/internal.h src/gc.h \
+ src/heapalloc.h src/debug_params.h
src/re_module.o: src/re_module.c src/alore.h src/value.h src/common.h \
src/aconfig.h config.h src/module.h src/thread.h src/globals.h \
src/errmsg.h src/re.h src/debug_params.h
View
33 src/reflect_module.c
@@ -10,7 +10,6 @@
#include "runtime.h"
#include "memberid.h"
#include "class.h"
-#include "std_module.h"
#include "str.h"
#include "internal.h"
#include "gc.h"
@@ -161,37 +160,7 @@ static AValue ReflectSetMember(AThread *t, AValue *frame)
/* reflect::TypeOf(obj) */
static AValue ReflectTypeOf(AThread *t, AValue *frame)
{
- /* IDEA: Combine with InternalType? */
- if (AIsInstance(frame[0])) {
- /* Anonymous functions need special processing. */
- if (AIsAnonFunc(frame[0]))
- return AGlobalByNum(AStdFunctionNum);
- else {
- /* This is the generic case. It covers all user-defined types
- and most C and library types. */
- AInstance *inst = AValueToInstance(frame[0]);
- return AGlobalByNum(AGetInstanceType(inst)->sym->num);
- }
- } else if (AIsInt(frame[0]))
- return AGlobalByNum(AStdIntNum);
- else if (AIsStr(frame[0]))
- return AGlobalByNum(AStdStrNum);
- else if (AIsFloat(frame[0]))
- return AGlobalByNum(AStdFloatNum);
- else if (AIsNil(frame[0]))
- return ARaiseValueError(t, "Type of nil not defined");
- else if (AIsConstant(frame[0]))
- return AGlobalByNum(AStdConstantNum);
- else if (AIsPair(frame[0]))
- return AGlobalByNum(AStdPairNum);
- else if (AIsRange(frame[0]))
- return AGlobalByNum(AStdRangeNum);
- else if (AIsOfType(frame[0], AGlobalByNum(AStdTypeNum)) == A_IS_TRUE)
- return AGlobalByNum(AStdTypeNum);
- else if (AIsGlobalFunction(frame[0]) || AIsMethod(frame[0]))
- return AGlobalByNum(AStdFunctionNum);
- else
- return ARaiseValueErrorND(t, NULL);
+ return AGetTypeObject(t, frame[0]);
}
View
37 src/runtime.c
@@ -299,6 +299,43 @@ ABool AIsSpecialType(AValue val)
}
+/* Return the type object that corresponds to the class of the object
+ argument. Raise ValueError if the argument is nil. */
+AValue AGetTypeObject(AThread *t, AValue object)
+{
+ if (AIsInstance(object)) {
+ /* Anonymous functions need special processing. */
+ if (AIsAnonFunc(object))
+ return AGlobalByNum(AStdFunctionNum);
+ else {
+ /* This is the generic case. It covers all user-defined types
+ and most C and library types. */
+ AInstance *inst = AValueToInstance(object);
+ return AGlobalByNum(AGetInstanceType(inst)->sym->num);
+ }
+ } else if (AIsInt(object))
+ return AGlobalByNum(AStdIntNum);
+ else if (AIsStr(object))
+ return AGlobalByNum(AStdStrNum);
+ else if (AIsFloat(object))
+ return AGlobalByNum(AStdFloatNum);
+ else if (AIsNil(object))
+ return ARaiseValueError(t, "Type of nil not defined");
+ else if (AIsConstant(object))
+ return AGlobalByNum(AStdConstantNum);
+ else if (AIsPair(object))
+ return AGlobalByNum(AStdPairNum);
+ else if (AIsRange(object))
+ return AGlobalByNum(AStdRangeNum);
+ else if (AIsOfType(object, AGlobalByNum(AStdTypeNum)) == A_IS_TRUE)
+ return AGlobalByNum(AStdTypeNum);
+ else if (AIsGlobalFunction(object) || AIsMethod(object))
+ return AGlobalByNum(AStdFunctionNum);
+ else
+ return ARaiseValueError(t, NULL);
+}
+
+
/* Compare two values for equality. Return AZero if the result is false,
AIntToValue(1) if true, and AError on error. */
AValue AIsEqual(AThread *t, AValue left, AValue right)
View
2 src/runtime.h
@@ -112,6 +112,8 @@ AValue ACoerce(AThread *t, AOperator op, AValue left, AValue right,
AValue AIsIn(AThread *t, AValue left, AValue right);
+AValue AGetTypeObject(AThread *t, AValue object);
+
AValue AWrapObject(AThread *thread, AValue obj);

0 comments on commit 4b7ffc0

Please sign in to comment.