Base for method reading.
[SquirrelJME.git] / nanocoat / src / tread.c
blobedb62e632db7e68ab3aec3540cde45eda28e67db
1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // -------------------------------------------------------------------------*/
10 #include <string.h>
12 #include "sjme/nvm/nvm.h"
13 #include "sjme/nvm/tread.h"
15 #define SJME_NVM_FRAME_TREAD_ACCESSOR_ADDRESS_GENERIC(cType, javaType, jType) \
16 sjme_errorCode SJME_TOKEN_PASTE_PP( \
17 sjme_nvm_frameTreadAccessorAddress, javaType)( \
18 sjme_attrInNotNull sjme_nvm_frame frame, \
19 sjme_attrInNotNull const sjme_nvm_frameTreadAccessor* accessor, \
20 sjme_attrInNotNull sjme_nvm_frameTread* tread, \
21 sjme_attrInPositive sjme_jint treadIndex, \
22 sjme_attrOutNotNull sjme_pointer* outAddress) \
23 { \
24 if (frame == NULL || accessor == NULL || tread == NULL || \
25 outAddress == NULL) \
26 return SJME_ERROR_NULL_ARGUMENTS; \
28 if (treadIndex < 0 || treadIndex >= tread->count) \
29 return SJME_ERROR_INVALID_ARGUMENT; \
31 /* Calculate address. */ \
32 *outAddress = &tread->values. \
33 SJME_TOKEN_PASTE_PP(jType, s)[treadIndex]; \
34 return SJME_ERROR_NONE; \
37 SJME_NVM_FRAME_TREAD_ACCESSOR_ADDRESS_GENERIC(sjme_jint, Integer, jint)
38 SJME_NVM_FRAME_TREAD_ACCESSOR_ADDRESS_GENERIC(sjme_jlong, Long, jlong)
39 SJME_NVM_FRAME_TREAD_ACCESSOR_ADDRESS_GENERIC(sjme_jfloat, Float, jfloat)
40 SJME_NVM_FRAME_TREAD_ACCESSOR_ADDRESS_GENERIC(sjme_jdouble, Double, jdouble)
41 SJME_NVM_FRAME_TREAD_ACCESSOR_ADDRESS_GENERIC(sjme_jobject, Object, jobject)
43 static sjme_errorCode sjme_nvm_frameTreadAccessorGetTreadGeneric(
44 sjme_attrInNotNull sjme_nvm_frame frame,
45 sjme_attrInNotNull const sjme_nvm_frameTreadAccessor* accessor,
46 sjme_attrInOutNotNull sjme_nvm_frameTread** outTread)
48 if (frame == NULL || accessor == NULL || outTread == NULL)
49 return SJME_ERROR_NULL_ARGUMENTS;
51 /* Get the base tread by its type ID. */
52 *outTread = frame->treads[accessor->typeId];
53 return SJME_ERROR_NONE;
56 static sjme_errorCode sjme_nvm_frameTreadAccessorReadGeneric(
57 sjme_attrInNotNull sjme_nvm_frame frame,
58 sjme_attrInNotNull const sjme_nvm_frameTreadAccessor* accessor,
59 sjme_attrInNotNull const sjme_nvm_frameTread* tread,
60 sjme_attrInPositive sjme_jint treadIndex,
61 sjme_attrOutNotNull sjme_pointer outVal)
63 sjme_pointer fromAddr;
65 if (frame == NULL || tread == NULL || outVal == NULL)
66 return SJME_ERROR_NULL_ARGUMENTS;
68 if (treadIndex < 0 || treadIndex >= tread->count)
69 return SJME_ERROR_INVALID_ARGUMENT;
71 /* Get the source address. */
72 fromAddr = NULL;
73 if (!accessor->address(frame, accessor, (sjme_nvm_frameTread*)tread,
74 treadIndex, &fromAddr) || fromAddr == NULL)
75 return SJME_ERROR_TREAD_INVALID_READ;
77 /* A memory operation can just be used here. */
78 memmove(outVal, fromAddr, accessor->size);
79 return SJME_JNI_TRUE;
82 static sjme_errorCode sjme_nvm_frameTreadAccessorWriteGeneric(
83 sjme_attrInNotNull sjme_nvm_frame frame,
84 sjme_attrInNotNull const sjme_nvm_frameTreadAccessor* accessor,
85 sjme_attrInNotNull sjme_nvm_frameTread* tread,
86 sjme_attrInPositive sjme_jint treadIndex,
87 sjme_attrInNotNull sjme_cpointer inVal)
89 sjme_pointer toAddr;
91 if (frame == NULL || tread == NULL || inVal == NULL)
92 return SJME_ERROR_NULL_ARGUMENTS;
94 if (treadIndex < 0 || treadIndex >= tread->count)
95 return SJME_ERROR_INVALID_ARGUMENT;
97 /* Get the target address. */
98 toAddr = NULL;
99 if (!accessor->address(frame, accessor, tread, treadIndex, &toAddr) ||
100 toAddr == NULL)
101 return SJME_ERROR_TREAD_INVALID_WRITE;
103 /* A memory operation can just be used here. */
104 memmove(toAddr, inVal, accessor->size);
105 return SJME_ERROR_NONE;
108 static const sjme_nvm_frameTreadAccessor sjme_nvm_constFrameTreadAccessor[
109 SJME_NUM_JAVA_TYPE_IDS] =
111 /* Integer. */
113 SJME_JAVA_TYPE_ID_INTEGER,
114 sizeof(sjme_jint),
115 "sjme_jint",
116 SJME_ERROR_TOP_NOT_INTEGER,
117 sjme_nvm_frameTreadAccessorGetTreadGeneric,
118 sjme_nvm_frameTreadAccessorAddressInteger,
119 sjme_nvm_frameTreadAccessorReadGeneric,
120 sjme_nvm_frameTreadAccessorWriteGeneric
123 /* Long. */
125 SJME_JAVA_TYPE_ID_LONG,
126 sizeof(sjme_jlong),
127 "sjme_jlong",
128 SJME_ERROR_TOP_NOT_LONG,
129 sjme_nvm_frameTreadAccessorGetTreadGeneric,
130 sjme_nvm_frameTreadAccessorAddressLong,
131 sjme_nvm_frameTreadAccessorReadGeneric,
132 sjme_nvm_frameTreadAccessorWriteGeneric
135 /* Float. */
137 SJME_JAVA_TYPE_ID_FLOAT,
138 sizeof(sjme_jfloat),
139 "sjme_jfloat",
140 SJME_ERROR_TOP_NOT_FLOAT,
141 sjme_nvm_frameTreadAccessorGetTreadGeneric,
142 sjme_nvm_frameTreadAccessorAddressFloat,
143 sjme_nvm_frameTreadAccessorReadGeneric,
144 sjme_nvm_frameTreadAccessorWriteGeneric
147 /* Double. */
149 SJME_JAVA_TYPE_ID_DOUBLE,
150 sizeof(sjme_jdouble),
151 "sjme_jdouble",
152 SJME_ERROR_TOP_NOT_DOUBLE,
153 sjme_nvm_frameTreadAccessorGetTreadGeneric,
154 sjme_nvm_frameTreadAccessorAddressDouble,
155 sjme_nvm_frameTreadAccessorReadGeneric,
156 sjme_nvm_frameTreadAccessorWriteGeneric
159 /* Object. */
161 SJME_JAVA_TYPE_ID_OBJECT,
162 sizeof(sjme_jobject),
163 "sjme_jobject",
164 SJME_ERROR_TOP_NOT_OBJECT,
165 sjme_nvm_frameTreadAccessorGetTreadGeneric,
166 sjme_nvm_frameTreadAccessorAddressObject,
167 sjme_nvm_frameTreadAccessorReadGeneric,
168 sjme_nvm_frameTreadAccessorWriteGeneric
172 const sjme_nvm_frameTreadAccessor* sjme_nvm_frameTreadAccessorByType(
173 sjme_attrInRange(SJME_JAVA_TYPE_ID_INTEGER, SJME_NUM_JAVA_TYPE_IDS - 1)
174 sjme_javaTypeId typeId)
176 /* Not valid at all? */
177 if (typeId < 0 || typeId >= SJME_NUM_JAVA_TYPE_IDS)
178 return NULL;
180 return &sjme_nvm_constFrameTreadAccessor[typeId];