diff --git a/Makefile b/Makefile index 757ec3e..c442c0f 100644 --- a/Makefile +++ b/Makefile @@ -1,48 +1,50 @@ -default: wasm - -# Common -ifdef DEBUG - COMPILER_OPTIONS=-g3 --profiling-funcs -s ASSERTIONS=1 -fsanitize=address -DNDEBUG - LINKER_OPTIONS=-Wl,--no-entry -else - COMPILER_OPTIONS=-fPIC -Oz -fno-exceptions -fno-rtti -fno-stack-protector -ffunction-sections -fdata-sections -fno-math-errno -DNDEBUG - LINKER_OPTIONS=-Wl,--gc-sections,--no-entry -endif - -SOURCES_CC = $(wildcard native/sweph/src/*.c) $(wildcard native/sweph/src/*.h) $(wildcard native/utils/*.c) $(wildcard native/utils/*.h) - -flutter: wasm - cd example && flutter build web - -test: test_flutter - -test_flutter: - cd example && flutter run - -publish: publish_flutter - -publish_flutter: - flutter analyze && flutter pub publish - -bump-version: - dart tool/bump_version.dart $(ARGS) - -# Wasm -COMPILED_EXPORTS="EXPORTED_FUNCTIONS=[\"_malloc\", \"_free\"]" - -ifneq ($(OS), Windows_NT) - USER_SPEC=-u $(shell id -u):$(shell id -g) -else - USER_SPEC= -endif - -wasm: assets/sweph.wasm - -assets/sweph.wasm: $(SOURCES_CC) - docker run --rm -v "$(CURDIR)/native/sweph/src:/src" -v "$(CURDIR)/native/utils:/src/utils" -v "$(CURDIR)/assets:/dist" $(USER_SPEC) \ - emscripten/emsdk \ - emcc -o /dist/sweph.wasm $(COMPILER_OPTIONS) $(LINKER_OPTIONS) \ - swecl.c swedate.c swehel.c swehouse.c swejpl.c swemmoon.c swemplan.c sweph.c swephlib.c utils/mem_io.c \ - -D fopen=fOpen -D fclose=fClose -D fread=fRead -D fwrite=fWrite -D rewind=fRewind -D fseek=fSeek -D ftell=fTell -D fgets=fGets -D printf=printF \ - -s 'EXPORT_NAME="sweph"' \ - -s $(COMPILED_EXPORTS) +default: wasm + +# Common +ifdef DEBUG + COMPILER_OPTIONS=-g3 --profiling-funcs -s ASSERTIONS=1 -fsanitize=address -DNDEBUG -DNO_SWE_GLP + LINKER_OPTIONS=-Wl,--no-entry,--export=__wasm_call_ctors +else + COMPILER_OPTIONS=-fPIC -Oz -fno-exceptions -fno-rtti -fno-stack-protector -ffunction-sections -fdata-sections -fno-math-errno -DNDEBUG -DNO_SWE_GLP + LINKER_OPTIONS=-Wl,--gc-sections,--no-entry,--export=__wasm_call_ctors +endif + +SOURCES_CC = $(wildcard native/sweph/src/*.c) $(wildcard native/sweph/src/*.h) $(wildcard native/utils/*.c) $(wildcard native/utils/*.h) + +flutter: wasm + cd example && flutter build web + +test: test_flutter + +test_flutter: + cd example && flutter run + +publish: publish_flutter + +publish_flutter: + flutter analyze && flutter pub publish + +bump-version: + dart tool/bump_version.dart $(ARGS) + +# Wasm +COMPILED_EXPORTS="EXPORTED_FUNCTIONS=[\"_malloc\", \"_free\", \"_write_file\"]" + +ifneq ($(OS), Windows_NT) + USER_SPEC=-u $(shell id -u):$(shell id -g) +else + USER_SPEC= +endif + +wasm: assets/sweph.wasm + +assets/sweph.wasm: $(SOURCES_CC) + docker run --rm -v "$(CURDIR)/native/sweph/src:/src" -v "$(CURDIR)/native/utils:/src/utils" -v "$(CURDIR)/assets:/dist" $(USER_SPEC) \ + emscripten/emsdk \ + emcc -o /dist/sweph.wasm $(COMPILER_OPTIONS) $(LINKER_OPTIONS) \ + swecl.c swedate.c swehel.c swehouse.c swejpl.c swemmoon.c swemplan.c sweph.c swephlib.c utils/mem_io.c \ + -D fopen=fOpen -D fclose=fClose -D fread=fRead -D fwrite=fWrite -D rewind=fRewind -D fseek=fSeek -D ftell=fTell -D fgets=fGets -D printf=printF -D getenv=wasmGetenv \ + -s STANDALONE_WASM=1 \ + -s ENVIRONMENT='web,worker' \ + -s FILESYSTEM=0 \ + -s $(COMPILED_EXPORTS) diff --git a/assets/sweph.wasm b/assets/sweph.wasm index 7168166..1ba5ba7 100755 Binary files a/assets/sweph.wasm and b/assets/sweph.wasm differ diff --git a/example.dart/pubspec.yaml b/example.dart/pubspec.yaml index 42b28b4..47851eb 100644 --- a/example.dart/pubspec.yaml +++ b/example.dart/pubspec.yaml @@ -4,7 +4,7 @@ publish_to: none version: 1.0.0 environment: - sdk: ^3.5.1 + sdk: ^3.10.8 dependencies: ffi: ^2.1.3 diff --git a/example.dart/web/main.dart b/example.dart/web/main.dart index 1d62779..1b69872 100644 --- a/example.dart/web/main.dart +++ b/example.dart/web/main.dart @@ -2,7 +2,7 @@ import 'package:example_dart/example.dart'; import 'package:web/web.dart'; void setValue(String id, String value) { - (document.querySelector('#$id') as HTMLElement).text = value; + (document.querySelector('#$id') as HTMLElement).textContent = value; } void main() { diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 01bc7c6..d5a530c 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -4,7 +4,7 @@ publish_to: "none" version: 1.0.0+1 environment: - sdk: ">=3.5.1 <4.0.0" + sdk: ">=3.10.8 <4.0.0" dependencies: flutter: @@ -24,4 +24,4 @@ flutter: uses-material-design: true assets: - - assets/ephe/seorbel.txt \ No newline at end of file + - assets/ephe/seorbel.txt diff --git a/lib/src/utils.dart b/lib/src/utils.dart index 89abd55..74065d5 100644 --- a/lib/src/utils.dart +++ b/lib/src/utils.dart @@ -112,12 +112,10 @@ extension Utf8Pointer on Pointer { String toDartString({int? length}) { _ensureNotNullptr('toDartString'); final codeUnits = cast(); - if (length != null) { - RangeError.checkNotNegative(length, 'length'); - } else { - length = _length(codeUnits); - } - return utf8.decode(codeUnits.asTypedList(length)); + final codeUnitsLength = length == null + ? _length(codeUnits) + : RangeError.checkNotNegative(length, 'length'); + return utf8.decode(codeUnits.asTypedList(codeUnitsLength)); } static int _length(Pointer codeUnits) { diff --git a/lib/src/wasm_asset_saver.dart b/lib/src/wasm_asset_saver.dart index fbac945..76e6130 100644 --- a/lib/src/wasm_asset_saver.dart +++ b/lib/src/wasm_asset_saver.dart @@ -3,9 +3,16 @@ import 'dart:typed_data'; import 'package:universal_ffi/ffi.dart'; import 'abstract_asset_saver.dart'; +typedef WriteFileNative = Int32 Function( + Pointer path, + Pointer contents, + Size len, + Int32 forceOverwrite, +); + typedef WriteFile = int Function( - int path, - int contents, + Pointer path, + Pointer contents, int len, int forceOverwrite, ); @@ -17,7 +24,9 @@ class SwephAssetSaver extends AbstractAssetSaver { late WriteFile writeFile; SwephAssetSaver._(super.epheFilesPath, this.library) { - writeFile = library.lookupFunction('write_file'); + writeFile = library + .lookup>('write_file') + .asFunction(); } static Future init( @@ -36,18 +45,17 @@ class SwephAssetSaver extends AbstractAssetSaver { final destPathPtr = _copyToWasm(Uint8List.fromList(destPath.codeUnits)); final dataPtr = _copyToWasm(contents); - // ignore: avoid_dynamic_calls - writeFile.call(destPathPtr, dataPtr, contents.length, 0); + writeFile(destPathPtr, dataPtr, contents.length, 0); - library.module.free(destPathPtr); + library.module.free(destPathPtr.address); } - int _copyToWasm(Uint8List data) { + Pointer _copyToWasm(Uint8List data) { final size = data.length; final dataPtr = library.module.malloc(size + 1); final memoryView = library.module.heap.asUint8List(); memoryView.setAll(dataPtr, data); memoryView[dataPtr + size] = 0; - return dataPtr; + return Pointer.fromAddress(dataPtr); } } diff --git a/native/utils/mem_io.c b/native/utils/mem_io.c index 78be55b..70af0f8 100644 --- a/native/utils/mem_io.c +++ b/native/utils/mem_io.c @@ -17,6 +17,11 @@ typedef struct File { File* files = NULL; +char* wasmGetenv(const char* name) { + (void)name; + return NULL; +} + int write_file(const char* path, char* contents, size_t len, int forceOverwrite) { File* file = (File*)fOpen(path, ""); if (file != NULL) { diff --git a/pubspec.yaml b/pubspec.yaml index 14c014c..ac181ce 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -5,12 +5,12 @@ homepage: https://github.com/vm75/sweph.dart repository: https://github.com/vm75/sweph.dart environment: - sdk: ^3.5.1 + sdk: ^3.10.8 flutter: ">=1.17.0" dependencies: path: ^1.9.0 - universal_ffi: ^1.1.0 + universal_ffi: ^1.5.0 dev_dependencies: ffigen: ^14.0.0