Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

网页交互 step1 #7

Merged
merged 4 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion inc/mm_heap.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
(m) = NULL; \
} while (0)

#define mheap_init() ((void)0)
#else
/**
* @brief 初始化堆分配器
Expand Down
5 changes: 3 additions & 2 deletions src/mm_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ static mstr_result_t mstr_expand_size(MString*, usize_t);
MSTR_EXPORT_API(mstr_result_t)
mstr_create(MString* str, const char* content)
{
usize_t content_len =
content[0] == '\0' ? 0 : (usize_t)strlen(content);
usize_t content_len = (content == NULL || content[0] == '\0') ?
0 :
(usize_t)strlen(content);
if (content_len == 0) {
str->buff = str->stack_region;
str->length = 0;
Expand Down
3 changes: 3 additions & 0 deletions www/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

.pyenv
.vscode
.svelte-kit
build
target
dist
Expand All @@ -24,3 +25,5 @@ node_modules
!*.json
!*.yml
!*.html
!*.js
!*.svelte
28 changes: 20 additions & 8 deletions www/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,29 @@
"license": "LGPL-3.0",
"private": true,
"scripts": {
"build": "yarn run webpack --mode=development",
"lint": "yarn run eslint ./src"
"lint": "yarn run eslint ./src",
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch"
},
"devDependencies": {
"@fontsource/fira-mono": "^4.5.10",
"@neoconfetti/svelte": "^1.0.0",
"@sveltejs/adapter-auto": "^2.0.0",
"@sveltejs/kit": "^1.5.0",
"@types/cookie": "^0.5.1",
"@typescript-eslint/eslint-plugin": "^5.59.6",
"@typescript-eslint/parser": "^5.59.6",
"eslint": "^8.40.0",
"html-webpack-plugin": "^5.5.1",
"ts-loader": "^9.4.2",
"typescript": "^5.0.4",
"webpack": "^5.82.1",
"webpack-cli": "^5.1.1"
}
"svelte": "^3.54.0",
"svelte-check": "^3.0.1",
"tslib": "^2.4.1",
"typescript": "^5.0.0",
"vite": "^4.3.0",
"vite-plugin-top-level-await": "^1.3.0",
"vite-plugin-wasm": "^3.2.2"
},
"type": "module"
}
12 changes: 12 additions & 0 deletions www/src/app.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// See https://kit.svelte.dev/docs/types#app
// for information about these interfaces
declare global {
namespace App {
// interface Error {}
// interface Locals {}
// interface PageData {}
// interface Platform {}
}
}

export { }
15 changes: 15 additions & 0 deletions www/src/app.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8" />
<!--<link rel="icon" href="%sveltekit.assets%/favicon.png" />-->
<meta name="viewport" content="width=device-width" />
%sveltekit.head%
</head>

<body data-sveltekit-preload-data="hover">
<div style="display: contents">%sveltekit.body%</div>
</body>

</html>
3 changes: 0 additions & 3 deletions www/src/index.ts

This file was deleted.

182 changes: 182 additions & 0 deletions www/src/mtfmt/lib.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
// SPDX-License-Identifier: LGPL-3.0
import MtfmtWASM from './mtfmt'

/**
* 库错误
*/
export enum MStrResult {
MStr_Ok = 0,
// ERR: 内存错误: 内存分配失败
MStr_Err_HeapTooSmall = -128,
// ERR: buff太短
MStr_Err_BufferTooSmall,
// ERR: 格式化: 索引太多
MStr_Err_IndexTooLarge,
// ERR: 格式化: Parser错误: 未识别的token
MStr_Err_UnrecognizedToken,
// ERR: 格式化: Parser错误: Missing `{`
MStr_Err_MissingLeftBrace,
// ERR: 格式化: Parser错误: Missing `}`
MStr_Err_MissingRightBrace,
// ERR: 格式化: Parser错误: Missing Formatter
MStr_Err_MissingReplacement,
// ERR: 格式化: Parser错误: Missing ArgID
MStr_Err_MissingArgumentID,
// ERR: 格式化: Parser错误: Missing ArgType
MStr_Err_MissingArgumentType,
// ERR: 格式化: Parser错误: 不支持的fill char
MStr_Err_UnsupportFillChar,
// ERR: 格式化: Parser错误: fill char后面必须要align
MStr_Err_MissingAlignAfterFillChar,
// ERR: 格式化: Parser错误: 不支持的格式化方式
MStr_Err_UnsupportFormatType,
// ERR: 格式化: Parser错误: 请求宽度太大
MStr_Err_WidthTooLarge,
// ERR: 格式化: Parser错误: 请求的格式化项太多(日期时间)
MStr_Err_TooMoreChronoItem,
// ERR: 格式化: Parser错误: 错失格式化的token(日期时间)
MStr_Err_MissingChronoItemType,
// ERR: 格式化: Parser错误: 未指定
MStr_Err_UndefinedParserError,
// ERR: 格式化: 还未使用过的参数ID
MStr_Err_UnusedArgumentID,
// ERR: 格式化: 参数太大
MStr_Err_InvaildArgumentID,
// ERR: 格式化: 类型不正确
MStr_Err_InvaildArgumentType,
// ERR: 格式化: 内建buffer不够大
MStr_Err_InternalBufferTooSmall,
// ERR: 格式化: 不支持的类型
MStr_Err_UnsupportType,
// ERR: 格式化: 不支持的量化精度
MStr_Err_UnsupportQuantBits,
// ERR: 最后一个的flag
MStr_Err_Flag_LastOne,
}

/**
* 取得库的wrapper版本号
*
* @returns 版本号
*/
export function wrap_version(): number {
return MtfmtWASM.mstr_wasm_version()
}

/**
* C对象接口
*/
export interface CObject {
destroy(): void
}

/**
* 字符串
*/
export class CString implements CObject {
private handle: number

constructor(content?: string) {
const handle = MtfmtWASM.mstr_wasm_new_string()
if (handle === 0) {
throw WebAssembly.RuntimeError('Cannot allocate memory for CString object')
}
// 初始化字符串
if (content !== undefined) {
this.handle = CString.cstr_cont(content, s => {
const code = MtfmtWASM.mstr_create(handle, s)
if (code !== MStrResult.MStr_Ok) {
throw WebAssembly.RuntimeError('Cannot create CString object')
}
return handle
})
} else {
const code = MtfmtWASM.mstr_create(handle, 0)
if (code !== MStrResult.MStr_Ok) {
throw WebAssembly.RuntimeError('Cannot create CString object')
}
this.handle = handle
}
}

/**
* 往末尾添加字符串
*
* @param str: 需要增加的字符串
*/
public append(str: string) {
const handle = this.handle
CString.cstr_cont(str, s => {
const code = MtfmtWASM.mstr_concat_cstr(handle, s)
if (code !== MStrResult.MStr_Ok) {
throw WebAssembly.RuntimeError('Cannot append to CString object')
}
})
}

/**
* 清空字符串
*/
public clear() {
MtfmtWASM.mstr_clear(this.handle)
}

/**
* 取得字符串内容
*
* @returns 字符串
*/
public get(): string {
const ret_handle = this.handle
// as cptr
const c_rawstr = MtfmtWASM.mstr_as_cstr(ret_handle)
// 取得字符串内容
const buffer = MtfmtWASM.memory.buffer
const length = MtfmtWASM.mstr_wasm_string_len(ret_handle)
const rawstr = new Uint8Array(buffer, c_rawstr, length)
// 取得字符串内容
const decoder = new TextDecoder()
const result = decoder.decode(rawstr)
return result
}

public destroy() {
const handle = this.handle
MtfmtWASM.mstr_free(handle)
MtfmtWASM.mstr_wasm_free_string(handle)
}

/**
* 将字符串转为cstr处理
*
* @param str: 字符串
* @param cont: cont
*/
private static cstr_cont<T>(str: string, cont: (str: number) => T): T {
const encoder = new TextEncoder()
const null_term = str + '\0'
const encoded_string = encoder.encode(null_term)
// 分配内存给字符串
const pstr = MtfmtWASM.mstr_wasm_allocate(encoded_string.length)
if (pstr === 0) {
throw WebAssembly.RuntimeError('Cannot allocate memory for c string')
}
// 处理内容
const dst = new Uint8Array(MtfmtWASM.memory.buffer, pstr)
dst.set(encoded_string)
const result = cont(pstr)
return result
}
}

/**
* 保证obj能被正确销毁的情况下执行代码
*
* @param obj: 对象
* @param cont: cont
*/
export function pcall<T extends CObject, R>(obj: T, cont: (obj: T) => R): R {
const r = cont(obj)
obj.destroy()
return r
}
25 changes: 25 additions & 0 deletions www/src/mtfmt/mtfmt.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-License-Identifier: LGPL-3.0

export declare function mstr_create(pstr: number, content: number): MStrResult

export declare function mstr_as_cstr(pstr: number): number

export declare function mstr_clear(pstr: number)

export declare function mstr_concat_cstr(pstr: number, cstr: number): MStrResult

export declare function mstr_free(pstr: number)

export declare function mstr_wasm_version(): number

export declare function mstr_wasm_new_string(): number

export declare function mstr_wasm_string_len(ptr: number): number

export declare function mstr_wasm_free_string(ptr: number)

export declare function mstr_wasm_allocate(size: number): number

export declare function mstr_wasm_free(ptr: number)

export declare const memory: WebAssembly.Memory
4 changes: 4 additions & 0 deletions www/src/mtfmt/mtfmt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// SPDX-License-Identifier: LGPL-3.0
import * as MtfmtWASM from '../../wasm/target/mtfmt.wasm'

export default MtfmtWASM
16 changes: 16 additions & 0 deletions www/src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!--
# SPDX-License-Identifier: LGPL-3.0
-->
<script>
import PreViewer from "./PreViewer.svelte";
</script>

<svelte:head>
<title>Mtfmt</title>
<meta name="description" content="MtFmt library" />
</svelte:head>

<section>
<h1>114514</h1>
<PreViewer />
</section>
3 changes: 3 additions & 0 deletions www/src/routes/+page.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// SPDX-License-Identifier: LGPL-3.0

export const prerender = true
10 changes: 10 additions & 0 deletions www/src/routes/PreViewer.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!--
# SPDX-License-Identifier: LGPL-3.0
-->
<script lang="ts">
function eval_test() {}
</script>

<div>
<button on:click={() => eval_test()}>114514</button>
</div>
14 changes: 0 additions & 14 deletions www/static/index.html

This file was deleted.

18 changes: 18 additions & 0 deletions www/svelte.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import adapter from '@sveltejs/adapter-auto';
import { vitePreprocess } from '@sveltejs/kit/vite';

/** @type {import('@sveltejs/kit').Config} */
const config = {
// Consult https://kit.svelte.dev/docs/integrations#preprocessors
// for more information about preprocessors
preprocess: vitePreprocess(),

kit: {
// adapter-auto only supports some environments, see https://kit.svelte.dev/docs/adapter-auto for a list.
// If your environment is not supported or you settled on a specific environment, switch out the adapter.
// See https://kit.svelte.dev/docs/adapters for more information about adapters.
adapter: adapter()
}
};

export default config;
Loading