add makeInt32List method (to create an Int32List actually backed by heap-allocated memory

This commit is contained in:
Nick Fisher
2025-05-13 22:37:29 +08:00
parent 25ada92574
commit 760ae8ed8b
3 changed files with 35 additions and 17 deletions

View File

@@ -11,6 +11,10 @@ const FILAMENT_SINGLE_THREADED = false;
const FILAMENT_WASM = false;
bool get IS_WINDOWS => Platform.isWindows;
Int32List makeInt32List(int length) {
return Int32List(length);
}
class NativeLibrary {
static void initBindings(String name) {
throw Exception();
@@ -93,8 +97,8 @@ void _voidCallbackHandler(int requestId) {
_requests[requestId]!.complete();
}
late NativeCallable<Void Function(Int32)>
_voidCallbackNativeCallable = NativeCallable<Void Function(Int32)>.listener(_voidCallbackHandler);
late NativeCallable<Void Function(Int32)> _voidCallbackNativeCallable =
NativeCallable<Void Function(Int32)>.listener(_voidCallbackHandler);
Future<void> withVoidCallback(
Function(int, Pointer<NativeFunction<Void Function(Int32)>>) func) async {
@@ -102,8 +106,9 @@ Future<void> withVoidCallback(
_requestId++;
final completer = Completer();
_requests[requestId] = completer;
_voidCallbackNativeCallable = NativeCallable<Void Function(Int32)>.listener(_voidCallbackHandler);
_voidCallbackNativeCallable =
NativeCallable<Void Function(Int32)>.listener(_voidCallbackHandler);
func.call(requestId, _voidCallbackNativeCallable.nativeFunction.cast());
await completer.future;

View File

@@ -10,6 +10,12 @@ const FILAMENT_SINGLE_THREADED = true;
const FILAMENT_WASM = true;
const IS_WINDOWS = false;
Int32List makeInt32List(int length) {
var ptr = malloc<Int32>(length * 4);
var buf = _NativeLibrary.instance._emscripten_make_int32_buffer(ptr, length);
return buf.toDart;
}
extension type _NativeLibrary(JSObject _) implements JSObject {
static _NativeLibrary get instance =>
NativeLibrary.instance as _NativeLibrary;
@@ -157,8 +163,9 @@ extension UInt32ListExtension on Uint32List {
return nullptr;
}
final ptr = getPointer<Uint32>(this, this.toJS);
final bar = Uint32ArrayWrapper(NativeLibrary.instance.HEAPU8, ptr, length)
as JSUint32Array;
final bar =
Uint32ArrayWrapper(NativeLibrary.instance.HEAPU8.buffer, ptr, length)
as JSUint32Array;
bar.toDart.setRange(0, length, this);
return ptr;
}
@@ -169,11 +176,17 @@ extension Int32ListExtension on Int32List {
if (this.lengthInBytes == 0) {
return nullptr;
}
final ptr = getPointer<Int32>(this, this.toJS);
final bar = Int32ArrayWrapper(NativeLibrary.instance.HEAPU8, ptr, length)
as JSInt32Array;
bar.toDart.setRange(0, length, this);
return ptr;
try {
this.buffer.asUint8List(this.offsetInBytes);
final ptr = getPointer<Int32>(this, this.toJS);
final bar =
Int32ArrayWrapper(NativeLibrary.instance.HEAPU8.buffer, ptr, length)
as JSInt32Array;
bar.toDart.setRange(0, length, this);
return ptr;
} catch (_) {
return Pointer<Int32>(this.offsetInBytes);
}
}
}
@@ -242,7 +255,7 @@ class CallbackHolder<T extends Function> {
extension DPCF on DartPickCallbackFunction {
CallbackHolder<DartPickCallbackFunction> asCallback() {
final ptr = addFunction<DartPickCallbackFunction>(this.toJS, "viidddd");
final ptr = addFunction<DartPickCallbackFunction>(this.toJS, "viiffff");
final cbh = CallbackHolder(ptr);
return cbh;
}
@@ -270,8 +283,7 @@ void Function(int) _voidCallback = (int requestId) {
final _voidCallbackPtr = _voidCallback.addFunction();
Future<void> withVoidCallback(
Future<void> withVoidCallback(
Function(int, Pointer<NativeFunction<Void Function(int)>>) func) async {
final completer = Completer();
final requestId = _completers.length;

View File

@@ -1,5 +1,6 @@
import 'package:animation_tools_dart/animation_tools_dart.dart';
import 'package:logging/logging.dart';
import 'package:thermion_dart/src/bindings/src/js_interop.dart';
import 'package:thermion_dart/src/utils/src/matrix.dart';
import 'package:thermion_dart/src/filament/src/implementation/ffi_filament_app.dart';
import 'package:thermion_dart/src/filament/src/implementation/ffi_material.dart';
@@ -56,10 +57,10 @@ class FFIAsset extends ThermionAsset {
Future<List<ThermionEntity>> getChildEntities() async {
if (_childEntities == null) {
var count = SceneAsset_getChildEntityCount(asset);
var childEntities = Int32List(count);
var childEntities = makeInt32List(count);
late Pointer stackPtr;
if (FILAMENT_WASM) {
//stackPtr = stackSave();
stackPtr = stackSave();
}
if (count > 0) {
SceneAsset_getChildEntities(asset, childEntities.address);
@@ -67,7 +68,7 @@ class FFIAsset extends ThermionAsset {
_childEntities = Int32List.fromList(childEntities);
childEntities.free();
if (FILAMENT_WASM) {
//stackRestore(stackPtr);
stackRestore(stackPtr);
}
}