mirror of
https://github.com/nunuhara/xsystem4.git
synced 2026-10-13 01:38:19 +03:00
Implement global save/load system calls
system.GlobalSave system.GlobalLoad system.GroupSave system.GroupLoad Save files are uncompressed JSON objects for now. Compatibility with System40.exe saves would be nice, but the format appears to be encrypted. Minifying & compressing the JSON should be done at some point but for debugging purposes I've left it totally uncompressed. This commit also fixes a bunch of undefined behavior when assigning to 'heap[...]' with an rvalue that could initiate a realloc of the heap.
This commit is contained in:
@@ -0,0 +1,288 @@
|
||||
/*
|
||||
Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef cJSON__h
|
||||
#define cJSON__h
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#if !defined(__WINDOWS__) && (defined(WIN32) || defined(WIN64) || defined(_MSC_VER) || defined(_WIN32))
|
||||
#define __WINDOWS__
|
||||
#endif
|
||||
|
||||
#ifdef __WINDOWS__
|
||||
|
||||
/* When compiling for windows, we specify a specific calling convention to avoid issues where we are being called from a project with a different default calling convention. For windows you have 3 define options:
|
||||
|
||||
CJSON_HIDE_SYMBOLS - Define this in the case where you don't want to ever dllexport symbols
|
||||
CJSON_EXPORT_SYMBOLS - Define this on library build when you want to dllexport symbols (default)
|
||||
CJSON_IMPORT_SYMBOLS - Define this if you want to dllimport symbol
|
||||
|
||||
For *nix builds that support visibility attribute, you can define similar behavior by
|
||||
|
||||
setting default visibility to hidden by adding
|
||||
-fvisibility=hidden (for gcc)
|
||||
or
|
||||
-xldscope=hidden (for sun cc)
|
||||
to CFLAGS
|
||||
|
||||
then using the CJSON_API_VISIBILITY flag to "export" the same symbols the way CJSON_EXPORT_SYMBOLS does
|
||||
|
||||
*/
|
||||
|
||||
#define CJSON_CDECL __cdecl
|
||||
#define CJSON_STDCALL __stdcall
|
||||
|
||||
/* export symbols by default, this is necessary for copy pasting the C and header file */
|
||||
#if !defined(CJSON_HIDE_SYMBOLS) && !defined(CJSON_IMPORT_SYMBOLS) && !defined(CJSON_EXPORT_SYMBOLS)
|
||||
#define CJSON_EXPORT_SYMBOLS
|
||||
#endif
|
||||
|
||||
#if defined(CJSON_HIDE_SYMBOLS)
|
||||
#define CJSON_PUBLIC(type) type CJSON_STDCALL
|
||||
#elif defined(CJSON_EXPORT_SYMBOLS)
|
||||
#define CJSON_PUBLIC(type) __declspec(dllexport) type CJSON_STDCALL
|
||||
#elif defined(CJSON_IMPORT_SYMBOLS)
|
||||
#define CJSON_PUBLIC(type) __declspec(dllimport) type CJSON_STDCALL
|
||||
#endif
|
||||
#else /* !__WINDOWS__ */
|
||||
#define CJSON_CDECL
|
||||
#define CJSON_STDCALL
|
||||
|
||||
#if (defined(__GNUC__) || defined(__SUNPRO_CC) || defined (__SUNPRO_C)) && defined(CJSON_API_VISIBILITY)
|
||||
#define CJSON_PUBLIC(type) __attribute__((visibility("default"))) type
|
||||
#else
|
||||
#define CJSON_PUBLIC(type) type
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* project version */
|
||||
#define CJSON_VERSION_MAJOR 1
|
||||
#define CJSON_VERSION_MINOR 7
|
||||
#define CJSON_VERSION_PATCH 12
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
/* cJSON Types: */
|
||||
#define cJSON_Invalid (0)
|
||||
#define cJSON_False (1 << 0)
|
||||
#define cJSON_True (1 << 1)
|
||||
#define cJSON_NULL (1 << 2)
|
||||
#define cJSON_Number (1 << 3)
|
||||
#define cJSON_String (1 << 4)
|
||||
#define cJSON_Array (1 << 5)
|
||||
#define cJSON_Object (1 << 6)
|
||||
#define cJSON_Raw (1 << 7) /* raw json */
|
||||
|
||||
#define cJSON_IsReference 256
|
||||
#define cJSON_StringIsConst 512
|
||||
|
||||
/* The cJSON structure: */
|
||||
typedef struct cJSON
|
||||
{
|
||||
/* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
|
||||
struct cJSON *next;
|
||||
struct cJSON *prev;
|
||||
/* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
|
||||
struct cJSON *child;
|
||||
|
||||
/* The type of the item, as above. */
|
||||
int type;
|
||||
|
||||
/* The item's string, if type==cJSON_String and type == cJSON_Raw */
|
||||
char *valuestring;
|
||||
/* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */
|
||||
int valueint;
|
||||
/* The item's number, if type==cJSON_Number */
|
||||
double valuedouble;
|
||||
|
||||
/* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
|
||||
char *string;
|
||||
} cJSON;
|
||||
|
||||
typedef struct cJSON_Hooks
|
||||
{
|
||||
/* malloc/free are CDECL on Windows regardless of the default calling convention of the compiler, so ensure the hooks allow passing those functions directly. */
|
||||
void *(CJSON_CDECL *malloc_fn)(size_t sz);
|
||||
void (CJSON_CDECL *free_fn)(void *ptr);
|
||||
} cJSON_Hooks;
|
||||
|
||||
typedef int cJSON_bool;
|
||||
|
||||
/* Limits how deeply nested arrays/objects can be before cJSON rejects to parse them.
|
||||
* This is to prevent stack overflows. */
|
||||
#ifndef CJSON_NESTING_LIMIT
|
||||
#define CJSON_NESTING_LIMIT 1000
|
||||
#endif
|
||||
|
||||
/* returns the version of cJSON as a string */
|
||||
CJSON_PUBLIC(const char*) cJSON_Version(void);
|
||||
|
||||
/* Supply malloc, realloc and free functions to cJSON */
|
||||
CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks);
|
||||
|
||||
/* Memory Management: the caller is always responsible to free the results from all variants of cJSON_Parse (with cJSON_Delete) and cJSON_Print (with stdlib free, cJSON_Hooks.free_fn, or cJSON_free as appropriate). The exception is cJSON_PrintPreallocated, where the caller has full responsibility of the buffer. */
|
||||
/* Supply a block of JSON, and this returns a cJSON object you can interrogate. */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value);
|
||||
/* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */
|
||||
/* If you supply a ptr in return_parse_end and parsing fails, then return_parse_end will contain a pointer to the error so will match cJSON_GetErrorPtr(). */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated);
|
||||
|
||||
/* Render a cJSON entity to text for transfer/storage. */
|
||||
CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item);
|
||||
/* Render a cJSON entity to text for transfer/storage without any formatting. */
|
||||
CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item);
|
||||
/* Render a cJSON entity to text using a buffered strategy. prebuffer is a guess at the final size. guessing well reduces reallocation. fmt=0 gives unformatted, =1 gives formatted */
|
||||
CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt);
|
||||
/* Render a cJSON entity to text using a buffer already allocated in memory with given length. Returns 1 on success and 0 on failure. */
|
||||
/* NOTE: cJSON is not always 100% accurate in estimating how much memory it will use, so to be safe allocate 5 bytes more than you actually need */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format);
|
||||
/* Delete a cJSON entity and all subentities. */
|
||||
CJSON_PUBLIC(void) cJSON_Delete(cJSON *item);
|
||||
|
||||
/* Returns the number of items in an array (or object). */
|
||||
CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array);
|
||||
/* Retrieve item number "index" from array "array". Returns NULL if unsuccessful. */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index);
|
||||
/* Get item "string" from object. Case insensitive. */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string);
|
||||
/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */
|
||||
CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void);
|
||||
|
||||
/* Check if the item is a string and return its valuestring */
|
||||
CJSON_PUBLIC(char *) cJSON_GetStringValue(const cJSON * const item);
|
||||
|
||||
/* These functions check the type of an item */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item);
|
||||
|
||||
/* These calls create a cJSON item of the appropriate type. */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string);
|
||||
/* raw json */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void);
|
||||
|
||||
/* Create a string where valuestring references a string so
|
||||
* it will not be freed by cJSON_Delete */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string);
|
||||
/* Create an object/array that only references it's elements so
|
||||
* they will not be freed by cJSON_Delete */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child);
|
||||
|
||||
/* These utilities create an Array of count items.
|
||||
* The parameter count cannot be greater than the number of elements in the number array, otherwise array access will be out of bounds.*/
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int count);
|
||||
|
||||
/* Append item to the specified array/object. */
|
||||
CJSON_PUBLIC(void) cJSON_AddItemToArray(cJSON *array, cJSON *item);
|
||||
CJSON_PUBLIC(void) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item);
|
||||
/* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the cJSON object.
|
||||
* WARNING: When this function was used, make sure to always check that (item->type & cJSON_StringIsConst) is zero before
|
||||
* writing to `item->string` */
|
||||
CJSON_PUBLIC(void) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item);
|
||||
/* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */
|
||||
CJSON_PUBLIC(void) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
|
||||
CJSON_PUBLIC(void) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item);
|
||||
|
||||
/* Remove/Detach items from Arrays/Objects. */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which);
|
||||
CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string);
|
||||
CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string);
|
||||
CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string);
|
||||
|
||||
/* Update array items. */
|
||||
CJSON_PUBLIC(void) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem); /* Shifts pre-existing items to the right. */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement);
|
||||
CJSON_PUBLIC(void) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem);
|
||||
CJSON_PUBLIC(void) cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem);
|
||||
CJSON_PUBLIC(void) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object,const char *string,cJSON *newitem);
|
||||
|
||||
/* Duplicate a cJSON item */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse);
|
||||
/* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will
|
||||
* need to be released. With recurse!=0, it will duplicate any children connected to the item.
|
||||
* The item->next and ->prev pointers are always zero on return from Duplicate. */
|
||||
/* Recursively compare two cJSON items for equality. If either a or b is NULL or invalid, they will be considered unequal.
|
||||
* case_sensitive determines if object keys are treated case sensitive (1) or case insensitive (0) */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive);
|
||||
|
||||
/* Minify a strings, remove blank characters(such as ' ', '\t', '\r', '\n') from strings.
|
||||
* The input pointer json cannot point to a read-only address area, such as a string constant,
|
||||
* but should point to a readable and writable adress area. */
|
||||
CJSON_PUBLIC(void) cJSON_Minify(char *json);
|
||||
|
||||
/* Helper functions for creating and adding items to an object at the same time.
|
||||
* They return the added item or NULL on failure. */
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * const name);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * const name, const cJSON_bool boolean);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * const name);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * const name);
|
||||
|
||||
/* When assigning an integer value, it needs to be propagated to valuedouble too. */
|
||||
#define cJSON_SetIntValue(object, number) ((object) ? (object)->valueint = (object)->valuedouble = (number) : (number))
|
||||
/* helper for the cJSON_SetNumberValue macro */
|
||||
CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number);
|
||||
#define cJSON_SetNumberValue(object, number) ((object != NULL) ? cJSON_SetNumberHelper(object, (double)number) : (number))
|
||||
|
||||
/* Macro for iterating over an array or object */
|
||||
#define cJSON_ArrayForEach(element, array) for(element = (array != NULL) ? (array)->child : NULL; element != NULL; element = element->next)
|
||||
|
||||
/* malloc/free objects using the malloc/free functions that have been set with cJSON_InitHooks */
|
||||
CJSON_PUBLIC(void *) cJSON_malloc(size_t size);
|
||||
CJSON_PUBLIC(void) cJSON_free(void *object);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
+1
-1
@@ -481,7 +481,7 @@ hll_warn_unimplemented(SACT2, Music_GetSampleLength, 1);
|
||||
// int Music_Seek(int nCh, int dwPos)
|
||||
hll_warn_unimplemented(SACT2, Music_Seek, 1);
|
||||
// int Sound_IsExist(int nNum)
|
||||
hll_unimplemented(SACT2, Sound_IsExist);
|
||||
hll_warn_unimplemented(SACT2, Sound_IsExist, 0);
|
||||
// int Sound_GetUnuseChannel(void)
|
||||
hll_unimplemented(SACT2, Sound_GetUnuseChannel);
|
||||
// int Sound_Prepare(int nCh, int nNum)
|
||||
|
||||
@@ -7,12 +7,14 @@ system4 = ['ain.c',
|
||||
'utfsjis.c',
|
||||
]
|
||||
xsystem4 = ['ald.c',
|
||||
'cJSON.c',
|
||||
'cg.c',
|
||||
'qnt.c',
|
||||
'ini.c',
|
||||
'input.c',
|
||||
'page.c',
|
||||
'sact_graphics.c',
|
||||
'savedata.c',
|
||||
'sdl_draw.c',
|
||||
'sdl_text.c',
|
||||
'sdl_video.c',
|
||||
|
||||
@@ -85,7 +85,7 @@ union vm_value variable_initval(enum ain_data_type type)
|
||||
return (union vm_value) { .i = -1 };
|
||||
case AIN_ARRAY_TYPE:
|
||||
slot = heap_alloc_slot(VM_PAGE);
|
||||
heap[slot].page = NULL;
|
||||
heap_set_page(slot, NULL);
|
||||
return (union vm_value) { .i = slot };
|
||||
default:
|
||||
return (union vm_value) { .i = 0 };
|
||||
@@ -123,7 +123,7 @@ enum ain_data_type array_type(enum ain_data_type type)
|
||||
}
|
||||
}
|
||||
|
||||
enum ain_data_type variable_type(struct page *page, int varno, enum ain_data_type *struct_type)
|
||||
enum ain_data_type variable_type(struct page *page, int varno, int *struct_type)
|
||||
{
|
||||
switch (page->type) {
|
||||
case GLOBAL_PAGE:
|
||||
@@ -169,19 +169,19 @@ struct page *copy_page(struct page *src)
|
||||
return dst;
|
||||
}
|
||||
|
||||
void alloc_struct(int no, union vm_value *var)
|
||||
int alloc_struct(int no)
|
||||
{
|
||||
struct ain_struct *s = &ain->structures[no];
|
||||
int slot = heap_alloc_slot(VM_PAGE);
|
||||
heap[slot].page = alloc_page(STRUCT_PAGE, no, s->nr_members);
|
||||
heap_set_page(slot, alloc_page(STRUCT_PAGE, no, s->nr_members));
|
||||
for (int i = 0; i < s->nr_members; i++) {
|
||||
if (s->members[i].data_type == AIN_STRUCT) {
|
||||
alloc_struct(s->members[i].struct_type, &heap[slot].page->values[i]);
|
||||
heap[slot].page->values[i].i = alloc_struct(s->members[i].struct_type);
|
||||
} else {
|
||||
heap[slot].page->values[i] = variable_initval(s->members[i].data_type);
|
||||
}
|
||||
}
|
||||
var->i = slot;
|
||||
return slot;
|
||||
}
|
||||
|
||||
void init_struct(int no, int slot)
|
||||
@@ -199,7 +199,7 @@ void init_struct(int no, int slot)
|
||||
|
||||
void create_struct(int no, union vm_value *var)
|
||||
{
|
||||
alloc_struct(no, var);
|
||||
var->i = alloc_struct(no);
|
||||
init_struct(no, var->i);
|
||||
}
|
||||
|
||||
@@ -222,7 +222,7 @@ struct page *alloc_array(int rank, union vm_value *dimensions, int data_type, in
|
||||
} else {
|
||||
struct page *child = alloc_array(rank - 1, dimensions + 1, data_type, struct_type, init_structs);
|
||||
int slot = heap_alloc_slot(VM_PAGE);
|
||||
heap[slot].page = child;
|
||||
heap_set_page(slot, child);
|
||||
page->values[i].i = slot;
|
||||
}
|
||||
}
|
||||
@@ -267,7 +267,7 @@ struct page *realloc_array(struct page *src, int rank, union vm_value *dimension
|
||||
} else {
|
||||
struct page *child = alloc_array(rank - 1, dimensions + 1, data_type, struct_type, init_structs);
|
||||
int slot = heap_alloc_slot(VM_PAGE);
|
||||
heap[slot].page = child;
|
||||
heap_set_page(slot, child);
|
||||
src->values[i].i = slot;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,7 +74,7 @@ static inline void page_set(int pageno, int varno, union vm_value v)
|
||||
// variables
|
||||
union vm_value variable_initval(enum ain_data_type type);
|
||||
void variable_fini(union vm_value v, enum ain_data_type type);
|
||||
enum ain_data_type variable_type(struct page *page, int varno, enum ain_data_type *struct_type);
|
||||
enum ain_data_type variable_type(struct page *page, int varno, int *struct_type);
|
||||
|
||||
// pages
|
||||
struct page *alloc_page(enum page_type type, int type_index, int nr_vars);
|
||||
@@ -83,11 +83,12 @@ struct page *copy_page(struct page *page);
|
||||
void delete_page(struct page *page);
|
||||
|
||||
// structs
|
||||
void alloc_struct(int no, union vm_value *var);
|
||||
int alloc_struct(int no);
|
||||
void init_struct(int no, int slot);
|
||||
void create_struct(int no, union vm_value *var);
|
||||
|
||||
// arrays
|
||||
enum ain_data_type array_type(enum ain_data_type type);
|
||||
struct page *alloc_array(int rank, union vm_value *dimensions, int data_type, int struct_type, bool init_structs);
|
||||
struct page *realloc_array(struct page *src, int rank, union vm_value *dimensions, int data_type, int struct_type, bool init_structs);
|
||||
int array_numof(struct page *page, int rank);
|
||||
|
||||
+352
@@ -0,0 +1,352 @@
|
||||
/* Copyright (C) 2019 Nunuhara Cabbage <nunuhara@haniwa.technology>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, see <http://gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include "system4.h"
|
||||
#include "savedata.h"
|
||||
#include "ain.h"
|
||||
#include "page.h"
|
||||
#include "vm.h"
|
||||
#include "cJSON.h"
|
||||
|
||||
#define invalid_save_data(msg, data) { \
|
||||
char *str = data ? cJSON_Print(data) : strdup("NULL"); \
|
||||
WARNING("Invalid save data: " msg ": %s", data); \
|
||||
free(str); \
|
||||
}
|
||||
|
||||
static cJSON *vm_value_to_json(enum ain_data_type type, union vm_value val);
|
||||
|
||||
static cJSON *page_to_json(int index)
|
||||
{
|
||||
struct page *page = vm_get_page(index);
|
||||
if (!page)
|
||||
return cJSON_CreateNull();
|
||||
|
||||
cJSON *values = cJSON_CreateArray();
|
||||
for (int i = 0; i < page->nr_vars; i++) {
|
||||
cJSON_AddItemToArray(values, vm_value_to_json(variable_type(page, i, NULL), page->values[i]));
|
||||
}
|
||||
return values;
|
||||
}
|
||||
|
||||
// FIXME: non-page reference types occupy two vm_values
|
||||
static cJSON *vm_value_to_json(enum ain_data_type type, union vm_value val)
|
||||
{
|
||||
switch (type) {
|
||||
case AIN_INT:
|
||||
case AIN_BOOL:
|
||||
case AIN_FUNC_TYPE:
|
||||
case AIN_DELEGATE:
|
||||
return cJSON_CreateNumber(val.i);
|
||||
case AIN_FLOAT:
|
||||
return cJSON_CreateNumber(val.f);
|
||||
case AIN_LONG_INT:
|
||||
return cJSON_CreateNumber(val.li);
|
||||
case AIN_STRING:
|
||||
return cJSON_CreateString(heap[val.i].s->text);
|
||||
case AIN_STRUCT:
|
||||
case AIN_ARRAY_TYPE:
|
||||
return page_to_json(val.i);
|
||||
case AIN_REF_TYPE:
|
||||
return cJSON_CreateNumber(-1);
|
||||
default:
|
||||
WARNING("Unhandled type: %d", ain_strtype(ain, type, -1));
|
||||
return cJSON_CreateNull();
|
||||
}
|
||||
}
|
||||
|
||||
struct global_save_data {
|
||||
cJSON *save;
|
||||
cJSON *globals;
|
||||
};
|
||||
|
||||
static void init_global_save_data(struct global_save_data *data, const char *keyname)
|
||||
{
|
||||
data->save = cJSON_CreateObject();
|
||||
data->globals = cJSON_CreateArray();
|
||||
cJSON_AddStringToObject(data->save, "key", keyname);
|
||||
cJSON_AddItemToObject(data->save, "globals", data->globals);
|
||||
}
|
||||
|
||||
static void add_global(struct global_save_data *data, int global, union vm_value val)
|
||||
{
|
||||
cJSON *obj = cJSON_CreateObject();
|
||||
cJSON_AddNumberToObject(obj, "index", global);
|
||||
cJSON_AddItemToObject(obj, "value", vm_value_to_json(ain->globals[global].data_type, val));
|
||||
cJSON_AddItemToArray(data->globals, obj);
|
||||
}
|
||||
|
||||
static int write_save_data(struct global_save_data *data, const char *filename)
|
||||
{
|
||||
FILE *f = fopen(filename, "w");
|
||||
if (!f) {
|
||||
WARNING("Failed to open save file: %s: %s", filename, strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *str = cJSON_Print(data->save);
|
||||
cJSON_Delete(data->save);
|
||||
|
||||
if (fwrite(str, strlen(str), 1, f) != 1) {
|
||||
WARNING("Failed to write save file: %s");
|
||||
free(str);
|
||||
return 0;
|
||||
}
|
||||
if (fclose(f)) {
|
||||
WARNING("Error writing save to file: %s: %s", filename, strerror(errno));
|
||||
free(str);
|
||||
return 0;
|
||||
}
|
||||
free(str);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int save_globals(const char *keyname, const char *filename)
|
||||
{
|
||||
struct global_save_data data;
|
||||
init_global_save_data(&data, keyname);
|
||||
|
||||
for (int i = 0; i < ain->nr_globals; i++) {
|
||||
add_global(&data, i, global_get(i));
|
||||
}
|
||||
|
||||
return write_save_data(&data, filename);
|
||||
}
|
||||
|
||||
static int get_group_index(const char *name)
|
||||
{
|
||||
for (int i = 0; i < ain->nr_global_groups; i++) {
|
||||
if (!strcmp(ain->global_group_names[i], name))
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int save_group(const char *keyname, const char *filename, const char *group_name, int *n)
|
||||
{
|
||||
int group;
|
||||
|
||||
if ((group = get_group_index(group_name)) < 0) {
|
||||
WARNING("Unregistered global group: %s", group_name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct global_save_data data;
|
||||
init_global_save_data(&data, keyname);
|
||||
|
||||
*n = 0;
|
||||
for (int i = 0; i < ain->nr_globals; i++) {
|
||||
if (ain->globals[i].group_index != group)
|
||||
continue;
|
||||
add_global(&data, i, global_get(i));
|
||||
(*n)++;
|
||||
}
|
||||
|
||||
return write_save_data(&data, filename);
|
||||
}
|
||||
|
||||
static union vm_value json_to_vm_value(enum ain_data_type type, enum ain_data_type struct_type, cJSON *json);
|
||||
|
||||
int get_json_array_rank(cJSON *json, union vm_value **dims)
|
||||
{
|
||||
// count rank
|
||||
int rank = 1;
|
||||
cJSON *array = json;
|
||||
while (cJSON_GetArraySize(array) > 0 && !cJSON_IsArray((array = cJSON_GetArrayItem(array, 0)))) {
|
||||
rank++;
|
||||
}
|
||||
|
||||
// count dims
|
||||
array = json;
|
||||
*dims = xmalloc(sizeof(union vm_value) * rank);
|
||||
for (int i = 0; i < rank; i++) {
|
||||
(*dims)[i].i = cJSON_GetArraySize(array);
|
||||
array = cJSON_GetArrayItem(array, 0);
|
||||
}
|
||||
|
||||
return rank;
|
||||
}
|
||||
|
||||
static void load_page(struct page *page, cJSON *vars)
|
||||
{
|
||||
int i = 0;
|
||||
cJSON *v;
|
||||
cJSON_ArrayForEach(v, vars) {
|
||||
int struct_type;
|
||||
enum ain_data_type data_type = variable_type(page, i, &struct_type);
|
||||
page->values[i] = json_to_vm_value(data_type, struct_type, v);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
static union vm_value json_to_vm_value(enum ain_data_type type, enum ain_data_type struct_type, cJSON *json)
|
||||
{
|
||||
char *str;
|
||||
int slot, rank;
|
||||
union vm_value *dims;
|
||||
switch (type) {
|
||||
case AIN_INT:
|
||||
case AIN_BOOL:
|
||||
if (!cJSON_IsNumber(json)) {
|
||||
invalid_save_data("Not a number", json);
|
||||
return vm_int(0);
|
||||
}
|
||||
return vm_int(json->valueint);
|
||||
case AIN_FLOAT:
|
||||
if (!cJSON_IsNumber(json)) {
|
||||
invalid_save_data("Not a number", json);
|
||||
return vm_float(0);
|
||||
}
|
||||
return vm_float(json->valuedouble);
|
||||
case AIN_LONG_INT:
|
||||
if (!cJSON_IsNumber(json)) {
|
||||
invalid_save_data("Not a number", json);
|
||||
return vm_long(0);
|
||||
}
|
||||
return vm_long(json->valueint);
|
||||
case AIN_STRING:
|
||||
slot = heap_alloc_slot(VM_STRING);
|
||||
if (!cJSON_IsString(json)) {
|
||||
invalid_save_data("Not a string", json);
|
||||
heap[slot].s = string_ref(&EMPTY_STRING);
|
||||
return vm_int(slot);
|
||||
}
|
||||
str = cJSON_GetStringValue(json);
|
||||
if (!str[0]) {
|
||||
heap[slot].s = string_ref(&EMPTY_STRING);
|
||||
} else {
|
||||
heap[slot].s = make_string(str, strlen(str));
|
||||
}
|
||||
return vm_int(slot);
|
||||
case AIN_STRUCT:
|
||||
slot = alloc_struct(struct_type);
|
||||
if (!cJSON_IsArray(json) || cJSON_GetArraySize(json) != ain->structures[struct_type].nr_members) {
|
||||
invalid_save_data("Not an array", json);
|
||||
} else {
|
||||
load_page(heap[slot].page, json);
|
||||
}
|
||||
return vm_int(slot);
|
||||
case AIN_ARRAY_TYPE:
|
||||
slot = heap_alloc_slot(VM_PAGE);
|
||||
if (cJSON_IsNull(json)) {
|
||||
heap[slot].page = NULL;
|
||||
return vm_int(slot);
|
||||
}
|
||||
if (!cJSON_IsArray(json)) {
|
||||
invalid_save_data("Not an array", json);
|
||||
heap[slot].page = NULL;
|
||||
return vm_int(slot);
|
||||
}
|
||||
rank = get_json_array_rank(json, &dims);
|
||||
heap[slot].page = alloc_array(rank, dims, array_type(type), struct_type, false);
|
||||
load_page(heap[slot].page, json);
|
||||
return vm_int(slot);
|
||||
case AIN_REF_TYPE:
|
||||
return vm_int(-1);
|
||||
default:
|
||||
WARNING("Unhandled data type: %s", ain_strtype(ain, type, -1));
|
||||
return vm_int(-1);
|
||||
}
|
||||
}
|
||||
|
||||
static cJSON *read_save_file(const char *filename)
|
||||
{
|
||||
FILE *f;
|
||||
long len;
|
||||
char *buf;
|
||||
|
||||
if (!(f = fopen(filename, "r"))) {
|
||||
WARNING("Failed to open save file: %s: %s", filename, strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fseek(f, 0, SEEK_END);
|
||||
len = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
|
||||
buf = xmalloc(len);
|
||||
if (fread(buf, len, 1, f) != 1) {
|
||||
WARNING("Failed to read save file: %s", filename);
|
||||
free(buf);
|
||||
return 0;
|
||||
}
|
||||
fclose(f);
|
||||
|
||||
cJSON *r = cJSON_Parse(buf);
|
||||
free(buf);
|
||||
return r;
|
||||
}
|
||||
|
||||
int load_globals(const char *keyname, const char *filename, const char *group_name, int *n)
|
||||
{
|
||||
int retval = 0;
|
||||
cJSON *save = read_save_file(filename);
|
||||
if (!save)
|
||||
return 0;
|
||||
if (!cJSON_IsObject(save)) {
|
||||
invalid_save_data("Not an object", save);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
cJSON *key = cJSON_GetObjectItem(save, "key");
|
||||
if (!key || strcmp(keyname, cJSON_GetStringValue(key)))
|
||||
VM_ERROR("Attempted to load save data with wrong key: %s", keyname);
|
||||
|
||||
if (group_name) {
|
||||
// TODO?
|
||||
}
|
||||
|
||||
cJSON *globals = cJSON_GetObjectItem(save, "globals");
|
||||
if (!globals || !cJSON_IsArray(globals)) {
|
||||
invalid_save_data("Not an array", globals);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
cJSON *g;
|
||||
cJSON_ArrayForEach(g, globals) {
|
||||
cJSON *index, *value;
|
||||
if (!(index = cJSON_GetObjectItem(g, "index"))) {
|
||||
invalid_save_data("Missing index", g);
|
||||
goto cleanup;
|
||||
}
|
||||
if (!(value = cJSON_GetObjectItem(g, "value"))) {
|
||||
invalid_save_data("Missing value", g);
|
||||
goto cleanup;
|
||||
}
|
||||
if (!cJSON_IsNumber(index)) {
|
||||
invalid_save_data("Not a number", index);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
int i = index->valueint;
|
||||
if (i < 0 || i > ain->nr_globals) {
|
||||
invalid_save_data("Invalid global index", index);
|
||||
goto cleanup;
|
||||
}
|
||||
NOTICE("LOADING GLOBAL %s (%s)", ain->globals[i].name, ain_strtype(ain, ain->globals[i].data_type, ain->globals[i].struct_type));
|
||||
global_set(i, json_to_vm_value(ain->globals[i].data_type, ain->globals[i].struct_type, value));
|
||||
if (n)
|
||||
(*n)++;
|
||||
}
|
||||
|
||||
retval = 1;
|
||||
cleanup:
|
||||
cJSON_Delete(save);
|
||||
return retval;
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
/* Copyright (C) 2019 Nunuhara Cabbage <nunuhara@haniwa.technology>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, see <http://gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef SYSTEM4_SAVEDATA_H
|
||||
#define SYSTEM4_SAVEDATA_H
|
||||
|
||||
int save_globals(const char *keyname, const char *filename);
|
||||
int save_group(const char *keyname, const char *filename, const char *group_name, int *n);
|
||||
int load_globals(const char *keyname, const char *filename, const char *group_name, int *n);
|
||||
|
||||
#endif /* SYSTEM4_SAVEDATA_H */
|
||||
@@ -26,6 +26,7 @@
|
||||
#include "ain.h"
|
||||
#include "instructions.h"
|
||||
#include "little_endian.h"
|
||||
#include "savedata.h"
|
||||
#include "utfsjis.h"
|
||||
|
||||
#define INITIAL_STACK_SIZE 4096
|
||||
@@ -165,6 +166,11 @@ void heap_unref(int slot)
|
||||
}
|
||||
}
|
||||
|
||||
void heap_set_page(int slot, struct page *page)
|
||||
{
|
||||
heap[slot].page = page;
|
||||
}
|
||||
|
||||
static int local_page_slot(void)
|
||||
{
|
||||
return call_stack[call_stack_ptr-1].page_slot;
|
||||
@@ -190,11 +196,26 @@ static union vm_value *local_ptr(int varno)
|
||||
return local_page() + varno;
|
||||
}
|
||||
|
||||
static union vm_value global_get(int varno)
|
||||
union vm_value global_get(int varno)
|
||||
{
|
||||
return heap[0].page->values[varno];
|
||||
}
|
||||
|
||||
void global_set(int varno, union vm_value val)
|
||||
{
|
||||
switch (ain->globals[varno].data_type) {
|
||||
case AIN_STRING:
|
||||
case AIN_STRUCT:
|
||||
case AIN_ARRAY_TYPE:
|
||||
if (heap[0].page->values[varno].i > 0) {
|
||||
heap_unref(heap[0].page->values[varno].i);
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
heap[0].page->values[varno] = val;
|
||||
}
|
||||
|
||||
static int32_t struct_page_slot(void)
|
||||
{
|
||||
return call_stack[call_stack_ptr-1].struct_page;
|
||||
@@ -205,6 +226,18 @@ static union vm_value *struct_page(void)
|
||||
return heap[struct_page_slot()].page->values;
|
||||
}
|
||||
|
||||
static bool page_index_valid(int index)
|
||||
{
|
||||
return index >= 0 && (size_t)index < heap_size && heap[index].ref > 0 && heap[index].type == VM_PAGE;
|
||||
}
|
||||
|
||||
struct page *vm_get_page(int index)
|
||||
{
|
||||
if (!page_index_valid(index))
|
||||
VM_ERROR("Invalid page index: %d", index);
|
||||
return heap[index].page;
|
||||
}
|
||||
|
||||
static union vm_value stack_peek(int n)
|
||||
{
|
||||
return stack[stack_ptr - (1 + n)];
|
||||
@@ -253,7 +286,7 @@ int vm_string_ref(struct string *s)
|
||||
int vm_copy_page(struct page *page)
|
||||
{
|
||||
int slot = heap_alloc_slot(VM_PAGE);
|
||||
heap[slot].page = copy_page(page);
|
||||
heap_set_page(slot, copy_page(page));
|
||||
return slot;
|
||||
}
|
||||
|
||||
@@ -289,7 +322,7 @@ static int alloc_scenario_page(const char *fname)
|
||||
f = &ain->functions[fno];
|
||||
|
||||
slot = heap_alloc_slot(VM_PAGE);
|
||||
heap[slot].page = alloc_page(LOCAL_PAGE, fno, f->nr_vars);
|
||||
heap_set_page(slot, alloc_page(LOCAL_PAGE, fno, f->nr_vars));
|
||||
for (int i = 0; i < f->nr_vars; i++) {
|
||||
heap[slot].page->values[i] = variable_initval(f->vars[i].data_type);
|
||||
}
|
||||
@@ -324,7 +357,7 @@ static void function_call(int fno, int return_address)
|
||||
{
|
||||
struct ain_function *f = &ain->functions[fno];
|
||||
int slot = heap_alloc_slot(VM_PAGE);
|
||||
heap[slot].page = alloc_page(LOCAL_PAGE, fno, f->nr_vars);
|
||||
heap_set_page(slot, alloc_page(LOCAL_PAGE, fno, f->nr_vars));
|
||||
|
||||
call_stack[call_stack_ptr++] = (struct function_call) {
|
||||
.fno = fno,
|
||||
@@ -424,6 +457,22 @@ static void system_call(int32_t code)
|
||||
case 0x0: // system.Exit(int nResult)
|
||||
vm_exit(stack_pop().i);
|
||||
break;
|
||||
case 0x1: { // system.GlobalSave(string szKeyName, string szFileName)
|
||||
int filename = stack_pop().i;
|
||||
int keyname = stack_pop().i;
|
||||
stack_push(save_globals(heap[keyname].s->text, heap[filename].s->text));
|
||||
heap_unref(filename);
|
||||
heap_unref(keyname);
|
||||
break;
|
||||
}
|
||||
case 0x2: { // system.GlobalLoad(string szKeyName, string szFileName)
|
||||
int filename = stack_pop().i;
|
||||
int keyname = stack_pop().i;
|
||||
stack_push(load_globals(heap[keyname].s->text, heap[filename].s->text, NULL, NULL));
|
||||
heap_unref(filename);
|
||||
heap_unref(keyname);
|
||||
break;
|
||||
}
|
||||
case 0x3: // system.LockPeek()
|
||||
case 0x4: // system.UnlockPeek()
|
||||
stack_push(1);
|
||||
@@ -435,16 +484,32 @@ static void system_call(int32_t code)
|
||||
free(utf);
|
||||
// XXX: caller S_POPs
|
||||
break;
|
||||
case 0xA: { // system.ExistsFile(string szFileName)
|
||||
int str = stack_pop().i;
|
||||
heap_unref(str);
|
||||
stack_push(1); // TODO
|
||||
break;
|
||||
}
|
||||
case 0xC:
|
||||
if (config.save_dir)
|
||||
stack_push_string(make_string(config.save_dir, strlen(config.save_dir)));
|
||||
else
|
||||
stack_push_string(string_ref(&EMPTY_STRING));
|
||||
break;
|
||||
case 0xD:
|
||||
case 0xD: // system.GetTime()
|
||||
stack_push(vm_time());
|
||||
break;
|
||||
case 0x13: {
|
||||
case 0xF:
|
||||
str = stack_peek_string(0);
|
||||
utf = sjis2utf(str->text, str->size);
|
||||
sys_warning("*GAME ERROR*: %s\n", utf);
|
||||
free(utf);
|
||||
// XXX: caller S_POPs
|
||||
break;
|
||||
case 0x11: // system.IsDebugMode()
|
||||
stack_push(0);
|
||||
break;
|
||||
case 0x13: { // system.GetFuncStackName(int nIndex)
|
||||
int i = call_stack_ptr - (1 + stack_pop().i);
|
||||
if (i < 0 || i >= call_stack_ptr) {
|
||||
const char *msg = "Invalid stack index";
|
||||
@@ -461,9 +526,31 @@ static void system_call(int32_t code)
|
||||
case 0x15: // system.Sleep(int nSleep)
|
||||
stack_pop();
|
||||
break;
|
||||
case 0x18: {
|
||||
union vm_value *n = stack_pop_var();
|
||||
int groupname = stack_pop().i;
|
||||
int filename = stack_pop().i;
|
||||
int keyname = stack_pop().i;
|
||||
stack_push(save_group(heap[keyname].s->text, heap[filename].s->text, heap[groupname].s->text, &n->i));
|
||||
heap_unref(groupname);
|
||||
heap_unref(filename);
|
||||
heap_unref(keyname);
|
||||
break;
|
||||
}
|
||||
case 0x19: { // system.GroupLoad(string szKeyName, string szFileName, string szGroupName, ref int nNumofLoad)
|
||||
union vm_value *n = stack_pop_var();
|
||||
int groupname = stack_pop().i;
|
||||
int filename = stack_pop().i;
|
||||
int keyname = stack_pop().i;
|
||||
NOTICE("LOADING %s", heap[filename].s->text);
|
||||
stack_push(load_globals(heap[keyname].s->text, heap[filename].s->text, heap[groupname].s->text, &n->i));
|
||||
heap_unref(groupname);
|
||||
heap_unref(filename);
|
||||
heap_unref(keyname);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
WARNING("Unimplemented syscall: 0x%X", code);
|
||||
vm_stack_trace();
|
||||
VM_ERROR("Unimplemented syscall: 0x%X", code);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -921,6 +1008,11 @@ static void execute_instruction(enum opcode opcode)
|
||||
stack_set(0, (int64_t)stack_peek(0).i);
|
||||
break;
|
||||
}
|
||||
case LI_ASSIGN: {
|
||||
int64_t v = stack_pop().li;
|
||||
stack_push(stack_pop_var()->li = v);
|
||||
break;
|
||||
}
|
||||
//
|
||||
// --- Floating Point Arithmetic ---
|
||||
//
|
||||
@@ -1211,7 +1303,7 @@ static void execute_instruction(enum opcode opcode)
|
||||
delete_page(heap[lval].page);
|
||||
free_page(heap[lval].page);
|
||||
}
|
||||
heap[lval].page = copy_page(heap[rval].page);
|
||||
heap_set_page(lval, copy_page(heap[rval].page));
|
||||
stack_push(rval);
|
||||
break;
|
||||
}
|
||||
@@ -1219,24 +1311,24 @@ static void execute_instruction(enum opcode opcode)
|
||||
// -- Arrays --
|
||||
//
|
||||
case A_ALLOC: {
|
||||
enum ain_data_type struct_type;
|
||||
int struct_type;
|
||||
int rank = stack_pop().i;
|
||||
int varno = stack_peek(rank).i;
|
||||
int pageno = stack_peek(rank+1).i;
|
||||
int array = heap[pageno].page->values[varno].i;
|
||||
enum ain_data_type data_type = variable_type(heap[pageno].page, varno, &struct_type);
|
||||
heap[array].page = alloc_array(rank, stack_peek_ptr(rank-1), data_type, struct_type, true);
|
||||
heap_set_page(array, alloc_array(rank, stack_peek_ptr(rank-1), data_type, struct_type, true));
|
||||
stack_ptr -= rank + 2;
|
||||
break;
|
||||
}
|
||||
case A_REALLOC: {
|
||||
enum ain_data_type struct_type;
|
||||
int struct_type;
|
||||
int rank = stack_pop().i; // rank
|
||||
int varno = stack_peek(rank).i;
|
||||
int pageno = stack_peek(rank+1).i;
|
||||
int array = heap[pageno].page->values[varno].i;
|
||||
enum ain_data_type data_type = variable_type(heap[pageno].page, varno, &struct_type);
|
||||
heap[array].page = realloc_array(heap[array].page, rank, stack_peek_ptr(rank-1), data_type, struct_type, true);
|
||||
heap_set_page(array, realloc_array(heap[array].page, rank, stack_peek_ptr(rank-1), data_type, struct_type, true));
|
||||
stack_ptr -= rank + 2;
|
||||
break;
|
||||
}
|
||||
@@ -1245,14 +1337,14 @@ static void execute_instruction(enum opcode opcode)
|
||||
if (heap[array].page) {
|
||||
delete_page(heap[array].page);
|
||||
free_page(heap[array].page);
|
||||
heap[array].page = NULL;
|
||||
heap_set_page(array, NULL);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case A_REF: {
|
||||
int array = stack_pop().i;
|
||||
int slot = heap_alloc_slot(VM_PAGE);
|
||||
heap[slot].page = copy_page(heap[array].page);
|
||||
heap_set_page(slot, copy_page(heap[array].page));
|
||||
stack_push(slot);
|
||||
break;
|
||||
}
|
||||
@@ -1281,7 +1373,7 @@ static void execute_instruction(enum opcode opcode)
|
||||
break;
|
||||
}
|
||||
case A_PUSHBACK: {
|
||||
enum ain_data_type struct_type;
|
||||
int struct_type;
|
||||
union vm_value val = stack_pop();
|
||||
int varno = stack_pop().i;
|
||||
int pageno = stack_pop().i;
|
||||
@@ -1306,7 +1398,7 @@ static void execute_instruction(enum opcode opcode)
|
||||
break;
|
||||
}
|
||||
case A_INSERT: {
|
||||
enum ain_data_type struct_type;
|
||||
int struct_type;
|
||||
union vm_value val = stack_pop();
|
||||
int i = stack_pop().i;
|
||||
int varno = stack_pop().i;
|
||||
@@ -1445,12 +1537,12 @@ void vm_execute_ain(struct ain *program)
|
||||
|
||||
// Initialize globals
|
||||
heap[0].ref = 1;
|
||||
heap[0].page = alloc_page(GLOBAL_PAGE, 0, ain->nr_globals);
|
||||
heap_set_page(0, alloc_page(GLOBAL_PAGE, 0, ain->nr_globals));
|
||||
for (int i = 0; i < ain->nr_globals; i++) {
|
||||
if (ain->globals[i].data_type == AIN_STRUCT) {
|
||||
// XXX: need to allocate storage for global structs BEFORE calling
|
||||
// constructors.
|
||||
alloc_struct(ain->globals[i].struct_type, &heap[0].page->values[i]);
|
||||
heap[0].page->values[i].i = alloc_struct(ain->globals[i].struct_type);
|
||||
} else {
|
||||
heap[0].page->values[i] = variable_initval(ain->globals[i].data_type);
|
||||
}
|
||||
|
||||
@@ -125,6 +125,12 @@ static inline void stack_push_value(union vm_value v)
|
||||
#define stack_push(v) (stack_push_value(vm_value_cast(v)))
|
||||
union vm_value stack_pop(void);
|
||||
|
||||
void heap_set_page(int slot, struct page *page);
|
||||
|
||||
union vm_value global_get(int varno);
|
||||
void global_set(int varno, union vm_value val);
|
||||
struct page *vm_get_page(int index);
|
||||
|
||||
int vm_string_ref(struct string *s);
|
||||
int vm_copy_page(struct page *page);
|
||||
union vm_value vm_copy(union vm_value v, enum ain_data_type type);
|
||||
|
||||
Reference in New Issue
Block a user