Skip to content
Open
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
98 changes: 50 additions & 48 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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)
Binary file modified assets/sweph.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion example.dart/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion example.dart/web/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -24,4 +24,4 @@ flutter:
uses-material-design: true

assets:
- assets/ephe/seorbel.txt
- assets/ephe/seorbel.txt
10 changes: 4 additions & 6 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,10 @@ extension Utf8Pointer on Pointer<Uint8> {
String toDartString({int? length}) {
_ensureNotNullptr('toDartString');
final codeUnits = cast<Uint8>();
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<Uint8> codeUnits) {
Expand Down
24 changes: 16 additions & 8 deletions lib/src/wasm_asset_saver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@ import 'dart:typed_data';
import 'package:universal_ffi/ffi.dart';
import 'abstract_asset_saver.dart';

typedef WriteFileNative = Int32 Function(
Pointer<Uint8> path,
Pointer<Uint8> contents,
Size len,
Int32 forceOverwrite,
);

typedef WriteFile = int Function(
int path,
int contents,
Pointer<Uint8> path,
Pointer<Uint8> contents,
int len,
int forceOverwrite,
);
Expand All @@ -17,7 +24,9 @@ class SwephAssetSaver extends AbstractAssetSaver<DynamicLibrary, Allocator> {
late WriteFile writeFile;

SwephAssetSaver._(super.epheFilesPath, this.library) {
writeFile = library.lookupFunction<WriteFile, WriteFile>('write_file');
writeFile = library
.lookup<NativeFunction<WriteFileNative>>('write_file')
.asFunction<WriteFile>();
}

static Future<SwephAssetSaver> init(
Expand All @@ -36,18 +45,17 @@ class SwephAssetSaver extends AbstractAssetSaver<DynamicLibrary, Allocator> {
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<Uint8> _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<Uint8>.fromAddress(dataPtr);
}
}
5 changes: 5 additions & 0 deletions native/utils/mem_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down