improve for wasm

This commit is contained in:
lixianjing 2023-03-06 18:04:11 +08:00
parent 1cb8cd29ec
commit 5a328c8c84
4 changed files with 181 additions and 104 deletions

View File

@ -15,7 +15,6 @@
#include "tkc/log.h"
#include "tkc/mem.h"
#include "tkc/utils.h"
#include "tkc/thread.h"
static log_level_t s_log_level = LOG_LEVEL_DEBUG;
@ -33,6 +32,9 @@ int32_t log_dummy(const char* fmt, ...) {
return 0;
}
#ifndef WITHOUT_FSCRIPT
#include "tkc/thread.h"
#define TK_LOG_BUFF_SIZE 255
static char* s_log_buff = NULL;
static void* s_debugger_log_ctx = NULL;
@ -70,3 +72,11 @@ ret_t log_set_debugger_hook(tk_debugger_log_t log, void* ctx) {
return RET_OK;
}
#else
ret_t log_notify_debugger(const char* format, ...) {
return RET_OK;
}
ret_t log_set_debugger_hook(tk_debugger_log_t log, void* ctx) {
return RET_OK;
}
#endif/*WITHOUT_FSCRIPT*/

View File

@ -23,7 +23,18 @@
#define TK_TKMEM_MANAGER_H
#include "tkc/types_def.h"
#ifdef WITH_WASM
void * realloc(void *ptr, size_t size);
#define TKMEM_ALLOC(size) malloc(size)
#define TKMEM_CALLOC(nmemb, size) calloc(nmemb, size)
#define TKMEM_REALLOC(p, size) realloc(p, size)
#define TKMEM_FREE(p) free((void*)p); p = NULL
#define TKMEM_ZALLOC(type) (type*)TKMEM_CALLOC(1, sizeof(type))
#define TKMEM_REALLOCT(type, p, n) (type*)realloc(p, (n) * sizeof(type))
#define TKMEM_ZALLOCN(type, n) (type*)calloc(n, sizeof(type))
#else
#include "tkc/platform.h"
#include "tkc/mem_allocator.h"
BEGIN_C_DECLS
@ -189,5 +200,6 @@ bool_t tk_mem_is_valid_addr(void* addr);
#define TK_IS_VALID_ADDR(addr) tk_mem_is_valid_addr(addr)
END_C_DECLS
#endif/*WITH_WASM*/
#endif /*TK_TKMEM_MANAGER_H*/

View File

@ -22,19 +22,69 @@
#ifndef TYPES_DEF_H
#define TYPES_DEF_H
#ifndef WITH_WASM
#include <math.h>
#include <time.h>
#include <ctype.h>
#include <wchar.h>
#include <errno.h>
#include <assert.h>
#include <memory.h>
#endif /*WITH_WASM*/
#include <stdarg.h>
#include <ctype.h>
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <inttypes.h>
#ifdef WITH_WASM
#include <stdio.h>
#if UINTPTR_MAX == 0xffffffff
#define __PRI64_PREFIX "ll"
#else
#define __PRI64_PREFIX "l"
#endif
#define PRIu64 __PRI64_PREFIX "u"
#define PRId64 __PRI64_PREFIX "d"
#define INFINITY 3.40282347E+38f
#define assert(__pp) wasm_assert(__pp, #__pp)
#define WITH_WCSXXX 1
#define WITHOUT_FSCRIPT
#define log_impl printf
#define floor(a) (int)(a)
#define abs(a) ((a) > 0 ? (a) : -(a))
#define fabs(a) ((a) > 0 ? (a) : -(a))
typedef int wchar_t;
int iswspace(wchar_t ch);
size_t wcslen(const wchar_t* s);
int wcscmp(const wchar_t* s1, const wchar_t* s2);
int wcscasecmp(const wchar_t* s1, const wchar_t* s2);
int wcsncmp(const wchar_t* s1, const wchar_t* s2, size_t n);
wchar_t* wcsdup(const wchar_t* s);
wchar_t* wcschr(const wchar_t* s, wchar_t c);
wchar_t* wcscpy(wchar_t* s1, const wchar_t* s2);
wchar_t* wcsncpy(wchar_t* s1, const wchar_t* s2, uint32_t n);
double atof(const char* str);
char* strrchr(const char* s, int c);
void wasm_assert(int p, const char* text);
int strcasecmp(const char* s1, const char* s2);
long strtol(const char* restrict str, char** restrict endptr, int base);
long long strtoll(const char* restrict str, char** restrict endptr, int base);
unsigned long strtoul(const char* restrict str, char** restrict endptr, int base);
unsigned long long strtoull(const char* restrict str, char** restrict endptr, int base);
#endif /*WITH_WASM*/
#if defined(__MINGW32__) || defined(__MINGW64__)
#ifndef MINGW
#define MINGW 1
@ -86,9 +136,9 @@ typedef int socklen_t;
#ifndef TK_WEAK
#if defined(__CC_ARM) || \
(defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)) /* ARM Compiler */
(defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)) /* ARM Compiler */
#define TK_WEAK __attribute__((weak))
#elif defined(__IAR_SYSTEMS_ICC__) /* for IAR Compiler */
#elif defined(__IAR_SYSTEMS_ICC__) /* for IAR Compiler */
#define TK_WEAK __weak
#elif defined(__GNUC__) && !defined(__MINGW32__) && !defined(__MINGW64__) /* GNU GCC Compiler */
#define TK_WEAK __attribute__((weak))

View File

@ -19,17 +19,20 @@
*
*/
#ifndef WITH_WASM
#include "tkc/fs.h"
#include "tkc/path.h"
#include "tkc/thread.h"
#include "tkc/data_reader_factory.h"
#include "tkc/data_writer_factory.h"
#endif/*WITH_WASM*/
#include "tkc/mem.h"
#include "tkc/utf8.h"
#include "tkc/wstr.h"
#include "tkc/path.h"
#include "tkc/utils.h"
#include "tkc/object.h"
#include "tkc/thread.h"
#include "tkc/named_value.h"
#include "tkc/data_reader_factory.h"
#include "tkc/data_writer_factory.h"
#define IS_ADDRESS_ALIGN_4(addr) !((((size_t)(addr)) & 0x3) | 0x0)
@ -632,92 +635,6 @@ static xml_property_close_state_t xml_property_get_close_state(const char* start
return close_state;
}
ret_t xml_file_expand(const char* filename, str_t* s, const char* data) {
str_t ss;
char subfilename[MAX_PATH + 1];
const char* start = data;
const char* p = strstr(start, INCLUDE_XML);
str_init(&ss, 1024);
while (p != NULL) {
/* 过滤在属性中的 INCLUDE_XML */
xml_property_close_state_t close_state = xml_property_get_close_state(start, p);
if (close_state == XML_PROPERTY_CLOSE_STATE_CLOSE) {
str_set(&ss, "");
str_append_with_len(s, start, p - start);
/*<include filename="subfilename">*/
while (*p != '\"' && *p != '\0') {
p++;
}
return_value_if_fail(*p == '\"', RET_FAIL);
p++;
while (*p != '\"' && *p != '\0') {
str_append_char(&ss, *p++);
}
return_value_if_fail(*p == '\"', RET_FAIL);
while (*p != '>' && *p != '\0') {
p++;
}
return_value_if_fail(*p == '>', RET_FAIL);
p++;
path_replace_basename(subfilename, MAX_PATH, filename, ss.str);
xml_file_expand_read(subfilename, &ss);
str_append(s, ss.str);
} else {
int size = 0;
char* str_end = NULL;
char* include_string_end = strstr(p, "?>");
if (close_state == XML_PROPERTY_CLOSE_STATE_OPEN_PROPERTY) {
str_end = TAG_PROPERTY;
size = tk_strlen(TAG_PROPERTY);
} else if (close_state == XML_PROPERTY_CLOSE_STATE_OPEN_SINGLE_QUOTE) {
size = 1;
str_end = "\'";
} else if (close_state == XML_PROPERTY_CLOSE_STATE_OPEN_DOUDLE_QUOTE) {
size = 1;
str_end = "\"";
}
if (str_end == NULL) {
log_error("do not find close property string !");
} else {
p = strstr(include_string_end, str_end) + size;
str_append_with_len(s, start, p - start);
}
}
start = p;
p = strstr(start, INCLUDE_XML);
}
str_append(s, start);
str_reset(&ss);
return RET_OK;
}
ret_t xml_file_expand_read(const char* filename, str_t* s) {
uint32_t size = 0;
char* buff = NULL;
return_value_if_fail(filename != NULL && s != NULL, RET_BAD_PARAMS);
str_set(s, "");
buff = (char*)file_read(filename, &size);
return_value_if_fail(buff != NULL, RET_FAIL);
if (strstr(buff, INCLUDE_XML) != NULL) {
xml_file_expand(filename, s, buff);
} else {
str_set_with_len(s, (const char*)buff, size);
}
TKMEM_FREE(buff);
return RET_OK;
}
ret_t tk_str_append(char* str, uint32_t max_len, const char* s) {
uint32_t len = 0;
uint32_t org_len = 0;
@ -1552,6 +1469,102 @@ char* tk_replace_char(char* str, char from, char to) {
return str;
}
uint32_t tk_strnlen(const char* str, uint32_t maxlen) {
const char* s;
return_value_if_fail(str != NULL, 0);
for (s = str; maxlen-- && *s != '\0'; ++s)
;
return s - str;
}
#ifndef WITH_WASM
ret_t xml_file_expand(const char* filename, str_t* s, const char* data) {
str_t ss;
char subfilename[MAX_PATH + 1];
const char* start = data;
const char* p = strstr(start, INCLUDE_XML);
str_init(&ss, 1024);
while (p != NULL) {
/* 过滤在属性中的 INCLUDE_XML */
xml_property_close_state_t close_state = xml_property_get_close_state(start, p);
if (close_state == XML_PROPERTY_CLOSE_STATE_CLOSE) {
str_set(&ss, "");
str_append_with_len(s, start, p - start);
/*<include filename="subfilename">*/
while (*p != '\"' && *p != '\0') {
p++;
}
return_value_if_fail(*p == '\"', RET_FAIL);
p++;
while (*p != '\"' && *p != '\0') {
str_append_char(&ss, *p++);
}
return_value_if_fail(*p == '\"', RET_FAIL);
while (*p != '>' && *p != '\0') {
p++;
}
return_value_if_fail(*p == '>', RET_FAIL);
p++;
path_replace_basename(subfilename, MAX_PATH, filename, ss.str);
xml_file_expand_read(subfilename, &ss);
str_append(s, ss.str);
} else {
int size = 0;
char* str_end = NULL;
char* include_string_end = strstr(p, "?>");
if (close_state == XML_PROPERTY_CLOSE_STATE_OPEN_PROPERTY) {
str_end = TAG_PROPERTY;
size = tk_strlen(TAG_PROPERTY);
} else if (close_state == XML_PROPERTY_CLOSE_STATE_OPEN_SINGLE_QUOTE) {
size = 1;
str_end = "\'";
} else if (close_state == XML_PROPERTY_CLOSE_STATE_OPEN_DOUDLE_QUOTE) {
size = 1;
str_end = "\"";
}
if (str_end == NULL) {
log_error("do not find close property string !");
} else {
p = strstr(include_string_end, str_end) + size;
str_append_with_len(s, start, p - start);
}
}
start = p;
p = strstr(start, INCLUDE_XML);
}
str_append(s, start);
str_reset(&ss);
return RET_OK;
}
ret_t xml_file_expand_read(const char* filename, str_t* s) {
uint32_t size = 0;
char* buff = NULL;
return_value_if_fail(filename != NULL && s != NULL, RET_BAD_PARAMS);
str_set(s, "");
buff = (char*)file_read(filename, &size);
return_value_if_fail(buff != NULL, RET_FAIL);
if (strstr(buff, INCLUDE_XML) != NULL) {
xml_file_expand(filename, s, buff);
} else {
str_set_with_len(s, (const char*)buff, size);
}
TKMEM_FREE(buff);
return RET_OK;
}
static uint64_t s_ui_thread_id = 0;
ret_t tk_set_ui_thread(uint64_t ui_thread_id) {
@ -1563,12 +1576,4 @@ ret_t tk_set_ui_thread(uint64_t ui_thread_id) {
bool_t tk_is_ui_thread(void) {
return s_ui_thread_id == tk_thread_self();
}
uint32_t tk_strnlen(const char* str, uint32_t maxlen) {
const char* s;
return_value_if_fail(str != NULL, 0);
for (s = str; maxlen-- && *s != '\0'; ++s)
;
return s - str;
}
#endif/*WITH_WASM*/