X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=ts%2Fstructs%2FChannelMessageHandler.ts;fp=ts%2Fstructs%2FChannelMessageHandler.ts;h=aff48916deda48b9c2066749c8e5c582d5f1e814;hb=fed2245c60159f6c074c9ed5c0f3ce273ad9841b;hp=52144c80955458149466f01ae90be06251168ac8;hpb=e9c2970adecaa14b3e83e11eb160a7bcbdae20da;p=ldk-java diff --git a/ts/structs/ChannelMessageHandler.ts b/ts/structs/ChannelMessageHandler.ts index 52144c80..aff48916 100644 --- a/ts/structs/ChannelMessageHandler.ts +++ b/ts/structs/ChannelMessageHandler.ts @@ -35,12 +35,14 @@ import * as bindings from '../bindings' // TODO: figure out location // todo: in-line interface filling handle_open_channel (their_node_id: Uint8Array, their_features: number, msg: number): void { const their_features_hu_conv: InitFeatures = new InitFeatures(null, their_features); + their_features_hu_conv.ptrs_to.add(this); const msg_hu_conv: OpenChannel = new OpenChannel(null, msg); arg.handle_open_channel(their_node_id, their_features_hu_conv, msg_hu_conv); }, handle_accept_channel (their_node_id: Uint8Array, their_features: number, msg: number): void { const their_features_hu_conv: InitFeatures = new InitFeatures(null, their_features); + their_features_hu_conv.ptrs_to.add(this); const msg_hu_conv: AcceptChannel = new AcceptChannel(null, msg); arg.handle_accept_channel(their_node_id, their_features_hu_conv, msg_hu_conv); }, @@ -164,12 +166,28 @@ import * as bindings from '../bindings' // TODO: figure out location public void handle_open_channel(Uint8Array their_node_id, InitFeatures their_features, OpenChannel msg) { bindings.ChannelMessageHandler_handle_open_channel(this.ptr, their_node_id, their_features == null ? 0 : their_features.ptr & ~1, msg == null ? 0 : msg.ptr & ~1); this.ptrs_to.add(their_features); + // Due to rust's strict-ownership memory model, in some cases we need to "move" + // an object to pass exclusive ownership to the function being called. + // In most cases, we avoid this being visible in GC'd languages by cloning the object + // at the FFI layer, creating a new object which Rust can claim ownership of + // However, in some cases (eg here), there is no way to clone an object, and thus + // we actually have to pass full ownership to Rust. + // Thus, after this call, their_features is reset to null and is now a dummy object. + their_features.ptr = 0; this.ptrs_to.add(msg); } public void handle_accept_channel(Uint8Array their_node_id, InitFeatures their_features, AcceptChannel msg) { bindings.ChannelMessageHandler_handle_accept_channel(this.ptr, their_node_id, their_features == null ? 0 : their_features.ptr & ~1, msg == null ? 0 : msg.ptr & ~1); this.ptrs_to.add(their_features); + // Due to rust's strict-ownership memory model, in some cases we need to "move" + // an object to pass exclusive ownership to the function being called. + // In most cases, we avoid this being visible in GC'd languages by cloning the object + // at the FFI layer, creating a new object which Rust can claim ownership of + // However, in some cases (eg here), there is no way to clone an object, and thus + // we actually have to pass full ownership to Rust. + // Thus, after this call, their_features is reset to null and is now a dummy object. + their_features.ptr = 0; this.ptrs_to.add(msg); }