mirror of
				https://github.com/CPunch/Cosmo.git
				synced 2025-10-31 04:50:12 +00:00 
			
		
		
		
	refactored internal strings
This commit is contained in:
		
							
								
								
									
										4
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								Makefile
									
									
									
									
									
								
							| @@ -1,8 +1,8 @@ | |||||||
| # make clean && make && ./bin/cosmo | # make clean && make && ./bin/cosmo | ||||||
|  |  | ||||||
| CC=clang | CC=clang | ||||||
| CFLAGS=-fPIE -O3 | CFLAGS=-fPIE -g3 #-O3 | ||||||
| LDFLAGS=#-fsanitize=address | LDFLAGS=-fsanitize=address | ||||||
| OUT=bin/cosmo | OUT=bin/cosmo | ||||||
|  |  | ||||||
| CHDR=\ | CHDR=\ | ||||||
|   | |||||||
| @@ -208,7 +208,10 @@ void markRoots(CState *state) { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     markTable(state, &state->globals); |     markTable(state, &state->globals); | ||||||
|     markObject(state, (CObj*)state->initString); |  | ||||||
|  |     // mark all internal strings | ||||||
|  |     for (int i = 0; i < INTERNALSTRING_MAX; i++) | ||||||
|  |         markObject(state, (CObj*)state->internalStrings[i]); | ||||||
|  |  | ||||||
|     traceGrays(state); |     traceGrays(state); | ||||||
| } | } | ||||||
|   | |||||||
							
								
								
									
										17
									
								
								src/cstate.c
									
									
									
									
									
								
							
							
						
						
									
										17
									
								
								src/cstate.c
									
									
									
									
									
								
							| @@ -34,8 +34,12 @@ CState *cosmoV_newState() { | |||||||
|     cosmoT_initTable(state, &state->strings, 8); // init string table |     cosmoT_initTable(state, &state->strings, 8); // init string table | ||||||
|     cosmoT_initTable(state, &state->globals, 8); // init global table |     cosmoT_initTable(state, &state->globals, 8); // init global table | ||||||
|  |  | ||||||
|     state->initString = NULL; |     // first, set all strings to NULL so our GC doesn't read garbage data | ||||||
|     state->initString = cosmoO_copyString(state, "__init", 6); |     for (int i = 0; i < INTERNALSTRING_MAX; i++) | ||||||
|  |         state->internalStrings[i] = NULL; | ||||||
|  |  | ||||||
|  |     // setup all strings used by the VM | ||||||
|  |     state->internalStrings[INTERNALSTRING_INIT] = cosmoO_copyString(state, "__init", 6); | ||||||
|     return state; |     return state; | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -51,11 +55,14 @@ void cosmoV_freeState(CState *state) { | |||||||
|         objs = next; |         objs = next; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     // free our string & global table |     // mark our internal VM strings NULL | ||||||
|     state->initString = NULL; |     for (int i = 0; i < INTERNALSTRING_MAX; i++) | ||||||
|  |         state->internalStrings[i] = NULL; | ||||||
|  |  | ||||||
|  |     // free our string & global table (the string table includes the internal VM strings) | ||||||
|     cosmoT_clearTable(state, &state->strings); |     cosmoT_clearTable(state, &state->strings); | ||||||
|     cosmoT_clearTable(state, &state->globals); |     cosmoT_clearTable(state, &state->globals); | ||||||
|  |      | ||||||
|     // free our gray stack & finally free the state structure |     // free our gray stack & finally free the state structure | ||||||
|     free(state->grayStack); |     free(state->grayStack); | ||||||
|     free(state); |     free(state); | ||||||
|   | |||||||
| @@ -12,6 +12,12 @@ typedef struct CCallFrame { | |||||||
|     CValue* base; |     CValue* base; | ||||||
| } CCallFrame; | } CCallFrame; | ||||||
|  |  | ||||||
|  | typedef enum { | ||||||
|  |     INTERNALSTRING_INIT, // __init | ||||||
|  |     INTERNALSTRING_EQUAL, // __equal | ||||||
|  |     INTERNALSTRING_MAX | ||||||
|  | } InternalStringEnum; | ||||||
|  |  | ||||||
| typedef struct CState { | typedef struct CState { | ||||||
|     bool panic; |     bool panic; | ||||||
|     int freezeGC; // when > 0, GC events will be ignored (for internal use) |     int freezeGC; // when > 0, GC events will be ignored (for internal use) | ||||||
| @@ -31,7 +37,7 @@ typedef struct CState { | |||||||
|     CCallFrame callFrame[FRAME_MAX]; // call frames |     CCallFrame callFrame[FRAME_MAX]; // call frames | ||||||
|     int frameCount; |     int frameCount; | ||||||
|  |  | ||||||
|     CObjString *initString; |     CObjString *internalStrings[INTERNALSTRING_MAX]; // strings used internally by the VM, eg. __init | ||||||
| } CState; | } CState; | ||||||
|  |  | ||||||
| COSMO_API CState *cosmoV_newState(); | COSMO_API CState *cosmoV_newState(); | ||||||
|   | |||||||
| @@ -187,7 +187,7 @@ COSMOVMRESULT cosmoV_call(CState *state, int args) { | |||||||
|             CValue ret; |             CValue ret; | ||||||
|  |  | ||||||
|             // check if they defined an initalizer |             // check if they defined an initalizer | ||||||
|             if (cosmoO_getObject(state, metaObj, cosmoV_newObj(state->initString), &ret) && IS_CLOSURE(ret)) { |             if (cosmoO_getObject(state, metaObj, cosmoV_newObj(state->internalStrings[INTERNALSTRING_INIT]), &ret) && IS_CLOSURE(ret)) { | ||||||
|                 CObjClosure *closure = cosmoV_readClosure(ret); |                 CObjClosure *closure = cosmoV_readClosure(ret); | ||||||
|  |  | ||||||
|                 if (args+1 != closure->function->args) { |                 if (args+1 != closure->function->args) { | ||||||
| @@ -202,8 +202,8 @@ COSMOVMRESULT cosmoV_call(CState *state, int args) { | |||||||
|                 if (!cosmoV_execute(state)) |                 if (!cosmoV_execute(state)) | ||||||
|                     return COSMOVM_RUNTIME_ERR; |                     return COSMOVM_RUNTIME_ERR; | ||||||
|                  |                  | ||||||
|                 // we throw away the return result, it's unused |                 // we throw away the return result, it's ignored | ||||||
|                 // pop the callframe and return result :) |                 // pop the callframe and return the new object :) | ||||||
|                 popCallFrame(state); |                 popCallFrame(state); | ||||||
|                 state->top++; // adjust stack back into place |                 state->top++; // adjust stack back into place | ||||||
|             } else { |             } else { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user