Merge linked list in-place in Rust - algorithm

I was working on a leetcode problem, where I need to merge the elements in the list
input: [0,3,1,0,4,5,2,0]
output: [4,11]
I have submitted the code by creating a new Linkedlist, but couldn't come up with in place solution in Rust. Where did I go wrong?
// Definition for singly-linked list.
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct ListNode {
pub val: i32,
pub next: Option<Box<ListNode>>
}
impl ListNode {
#[inline]
fn new(val: i32) -> Self {
ListNode {
next: None,
val
}
}
}
struct Solution;
impl Solution {
pub fn merge_nodes(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
if head.is_none() {return None}
type Bl = Box<ListNode>;
let mut dummy: Bl = Box::new(ListNode::new(0));
// Assuming first element is 0 & non-empty list
let mut src_tail: &mut Bl = &mut head.unwrap();
let mut res_tail: &mut Bl = &mut dummy;
let mut sum = 0;
// LOOPING src list till end
while let Some(ref mut node) = src_tail.next {
sum += node.val;
// if zero then append the accumulated sum to res_tail
if node.val == 0 {
res_tail.next = Some(Box::new(ListNode::new(sum)));
sum = 0;
if let Some(ref mut post) = res_tail.next {
res_tail = post;
}
}
src_tail = node;
}
dummy.next
}
pub fn merge_nodes_in_place(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
type List = Box<ListNode>;
if head.is_none() {return None}
let res = head.clone();
let mut start: List = head.unwrap();
while let Some(node) = start.next {
let mut sum = 0;
let mut end: List = node;
while end.val != 0 {
sum += end.val;
let tail = end.next.take();
match tail {
Some(next_node) => {
end = next_node.next.unwrap();
},
None => ()
}
}
// end is pointing to 0th Node or next item is None
end.val = sum;
if end.next.is_none() {break;}
start = end.next.unwrap();
}
res
}
}
fn main() {
let a = Some(Box::new(ListNode { val: 0, next: None }));
let a = Some(Box::new(ListNode { val: 2, next: a }));
let a = Some(Box::new(ListNode { val: 5, next: a }));
let a = Some(Box::new(ListNode { val: 4, next: a }));
let a = Some(Box::new(ListNode { val: 0, next: a }));
let a = Some(Box::new(ListNode { val: 1, next: a }));
let a = Some(Box::new(ListNode { val: 3, next: a }));
let head = Some(Box::new(ListNode { val: 0, next: a }));
println!("{:?}", Solution::merge_nodes(head));
// Some(ListNode { val: 4, next: Some(ListNode { val: 11, next: None }) })
let a = Some(Box::new(ListNode { val: 0, next: None }));
let a = Some(Box::new(ListNode { val: 2, next: a }));
let a = Some(Box::new(ListNode { val: 5, next: a }));
let a = Some(Box::new(ListNode { val: 4, next: a }));
let a = Some(Box::new(ListNode { val: 0, next: a }));
let a = Some(Box::new(ListNode { val: 1, next: a }));
let a = Some(Box::new(ListNode { val: 3, next: a }));
let head = Some(Box::new(ListNode { val: 0, next: a }));
println!("{:?}", Solution::merge_nodes_in_place(head));
}

impl Solution {
fn consume_next_node(cursor: &mut ListNode)->Option<&mut ListNode> {
let next = cursor.next.as_mut()?;
if next.next.is_none() || next.val != 0 {
cursor.val += next.val;
let nextnext = next.next.take();
cursor.next = nextnext;
Some(cursor)
} else {
cursor.next.as_mut().map(|b| &mut **b)
}
}
pub fn merge_nodes_in_place(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut cursor: Option<&mut ListNode> = head.as_mut().map(|b| &mut **b);
while let Some(node) = cursor {
cursor = Solution::consume_next_node(node);
}
head
}
}

Related

How get discriminator from anchor account?

I have the contract
use anchor_lang::prelude::*;
declare_id!("kek");
#[constant]
pub const MEDIA_TAG: &[u8] = b"MEDIA_STATE";
#[account]
#[derive(Default)]
pub struct MediaState {
pub authority: Pubkey,
pub name: String,
}
#[program]
pub mod publisher {
use super::*;
pub fn create_media(ctx: Context<CreateMedia>, name: String) -> Result<()> {
msg!("Media name: {}", name);
let media_state = &mut ctx.accounts.media_state;
media_state.authority = ctx.accounts.authority.key();
media_state.name = name;
msg!("Media created");
Ok(())
}
}
#[derive(Accounts)]
#[instruction()]
pub struct CreateMedia<'info> {
#[account(mut)]
pub authority: Signer<'info>,
#[account(
init,
seeds = [MEDIA_TAG, authority.key().as_ref()],
bump,
payer = authority,
space = 8 + std::mem::size_of::<MediaState>(),
)]
pub media_state: Box<Account<'info, MediaState>>,
pub system_program: Program<'info, System>,
}
and I`d like to get the MediaState/CreateMedia discriminator in my javascript code
const publisher = new web3.PublicKey(
"kek"
);
const connection = new web3.Connection(web3.clusterApiUrl("devnet"));
const accounts = await connection.getProgramAccounts(publisher, {
filters: [{ memcmp: { offset: 0, bytes: ?????????? } }],
});
I don`t want use anchor in my js code bc i need only the getProgramAccounts
Thanks
filters: [{ memcmp: { offset: 0, bytes: "FtoqZ3bt1he" } }] - it works. But I don`t know how I can get "FtoqZ3bt1he".
I stole it from SolanaPlayground (network devtools)
const name = 'MediaState';
const discriminator = Buffer.from(sha256.digest("account:" + name)).slice(
0,
8
);
const publisher = new web3.PublicKey(
"kek"
);
const connection = new web3.Connection(web3.clusterApiUrl("devnet"));
const accounts = await connection.getProgramAccounts(publisher, {
filters: [{ memcmp: { offset: 0, bytes: base58.encode(discriminator) } }],
});

RXSwift why loading variable return wrong value?

Here is my view model:
let loadMoreTrigger = PublishSubject<Void>()
let refreshTrigger = PublishSubject<Void>()
let loading = BehaviorRelay<Bool>(value: false)
let stories = BehaviorRelay<[Story]>(value: [])
var offset = 0
let error = PublishSubject<String>()
let selectedFeedType: BehaviorRelay<FeedType> = BehaviorRelay(value: .best)
override init() {
super.init()
let refreshRequest = loading.asObservable().sample(refreshTrigger).flatMap { loading -> Observable<[Story]> in
if loading {
return Observable.empty()
} else {
self.offset = 0
return self.fetchStories(type: self.selectedFeedType.value, offset: self.offset)
}
}
let loadMoreRequest = loading.asObservable().sample(loadMoreTrigger).flatMap { loading -> Observable<[Story]> in
if loading {
return Observable.empty()
} else {
self.offset += 10
return self.fetchStories(type: self.selectedFeedType.value, offset: self.offset)
}
}
let request = Observable.merge(refreshRequest, loadMoreRequest).share(replay: 1)
let response = request.flatMap { (stories) -> Observable<[Story]> in
request.do(onError: { error in
self.error.onNext(error.localizedDescription)
}).catchError { (error) -> Observable<[Story]> in
Observable.empty()
}
}.share(replay: 1)
Observable.combineLatest(request, response, stories.asObservable()) { request, response, stories in
return self.offset == 0 ? response : stories + response
}.sample(response).bind(to: stories).disposed(by: disposeBag)
Observable.merge(request.map{_ in true}, response.map{_ in false}, error.map{_ in false}).bind(to: loading).disposed(by: disposeBag)
}
Then when i checking loading observer i have false -> true, instead of true -> false. I just don't understand why it happening.
loading.subscribe {
print($0)
}.disposed(by: disposeBag)
In my viewController i call refreshTrigger on viewWillAppear using rx.sentMessage
Here is getFeed function:
func getFeed(type: FeedType, offset: Int) -> Observable<[Story]> {
return provider.rx.request(.getFeed(type: type, offset: offset)).asObservable().flatMap { (response) -> Observable<[Story]> in
do {
let feedResponse = try self.jsonDecoder.decode(BaseAPIResponse<[Story]>.self, from: response.data)
guard let stories = feedResponse.data else { return .error(APIError.requestFailed)}
return .just(stories)
} catch {
return .error(error)
}
}.catchError { (error) -> Observable<[Story]> in
return .error(error)
}
}
Your request and response observables are emitting values at the exact same time. Which one shows up in your subscribe first is undefined.
Specifically, request doesn't emit a value until after the fetch request completes. Try this instead:
Observable.merge(
loadMoreTrigger.map { true },
refreshTrigger.map { true },
response.map { _ in false },
error.map { _ in false }
)
.bind(to: loading)
.disposed(by: disposeBag)
There are lots of other problems in your code but the above answers this specific question.

RxJS v7: How to dynamically add/remove and pause/resume observables?

Given some arrays:
const recordings = {
foo: [{delay: 5, data: 'a'}, {delay: 2, data: 'b'}],
bar: [{delay: 3, data: 'x'}, {delay: 7, data: 'y'}, {delay: 4, data: 'z'}]
};
Is there a way that I can pass them to a playback method that will do something like the following?:
beginPlayback(timeline, fromIndex = 0) {
let postedIndex = -1;
const observable = from(timeline.slice(fromIndex)).pipe(
concatMap((instant, i) => of({ i, instant }).pipe(delay(instant.delay))),
tap(async ({i, instant}) => {
try {
await post(instant.data);
postedIndex = i;
} catch (err) {
console.error(err);
// Continue playback
}
})
);
const handle = {
postedIndex,
subscription: observable.subscribe()
};
return handle;
}
...and then allow me to manage concurrent playback with an external/public interface like this?:
const handles = {};
const paused = {};
onAddNewRecordingRequest(id, timeline) {
recordings[id] = timeline;
}
onBeginPlaybackRequest(id) {
handles[id] = beginPlayback(recordings[id]);
}
onPauseRequested(id) {
const handle = handles[id];
paused[id] = handle.postedIndex;
handle.subscription.unsubscribe() // dispose of observable
}
onResumeRequested(id) {
const alreadyThroughIndex = paused[id];
handles[id] = beginPlayback(recordings[id], alreadyThroughIndex);
delete paused[id];
}
onStopRequested(id) {
const handle = handles[id];
handle.subscription.unsubscribe() // dispose of observable
}
Is there also a way to automatically remove the observable and the handle when the observable is completed?

LinkError: WebAssembly.instantiate(): Import #1 module="go" function="runtime.resetMemoryDataView" error: function import requires a callable

As I'm building my Web assembly application I have bumped into issue with a cryptic error:
LinkError: WebAssembly.instantiate(): Import #1 module="go" function="runtime.resetMemoryDataView" error: function import requires a callable
It is compiled with this command:
GOOS=js GOARCH=wasm go build -o main.wasm main.go server.go
This is the body of index.html, there is nothing in
<body class="is-preload">
<script src="wasm_exec.js"></script>
<script>
wasm_filename = "main.wasm";
function message(s){
document.getElementById("message").textContent = s;
}
function load_wasm(){
if (!WebAssembly.instantiateStreaming) { // polyfill
WebAssembly.instantiateStreaming = async (resp, importObject) => {
const source = await (await resp).arrayBuffer();
return await WebAssembly.instantiate(source, importObject);
};
}
const go = new Go();
WebAssembly.instantiateStreaming(fetch(wasm_filename), go.importObject)
.then(results => { go.run(results.instance); })
.catch((err) => {
message("Error Loading WebAssembly - " + err);
console.error(err);
// location.reload(true);
});
}
load_wasm()
</script>
This is main.go:
import (
"fmt"
"strconv"
"syscall/js"
)
func key(this js.Value, arg []js.Value) interface{} {
arg[0].Call("stopPropagation")
arg[0].Call("preventDefault")
return nil
}
func sum(this js.Value, args []js.Value) interface{} {
var rv interface{}
value1 := js.Global().Get("document").Call("getElementById", args[0].String()).Get("value").String()
value2 := js.Global().Get("document").Call("getElementById", args[1].String()).Get("value").String()
int1, _ := strconv.Atoi(value1)
int2, _ := strconv.Atoi(value2)
js.Global().Get("document").Call("getElementById", "result").Set("value", int1+int2)
return rv
}
func register_callbacks() {
js.Global().Set("key", js.FuncOf(key))
js.Global().Set("sum", js.FuncOf(sum))
}
func init() {
register_callbacks()
fmt.Printf("WebAssembly program started\n")
select {}
}
Then we have the server:
package main
import (
"flag"
"fmt"
"net/http"
)
var listen = flag.String("listen", ":8081", "listen address")
var dir = flag.String("dir", ".", "directory to serve")
func main() {
flag.Parse()
fs := http.FileServer(http.Dir("./assets/"))
http.Handle("/", fs)
fmt.Printf("Web server running. Listening on %q", *listen)
err := http.ListenAndServe(*listen, http.FileServer(http.Dir(*dir)))
fmt.Printf("%v\n", err)
}
This is wasm_exec.js:
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
(() => {
if (typeof global !== "undefined") {
// global already exists
} else if (typeof window !== "undefined") {
window.global = window;
} else if (typeof self !== "undefined") {
self.global = self;
} else {
throw new Error("cannot export Go (neither global, window nor self is defined)");
}
// Map web browser API and Node.js API to a single common API (preferring web standards over Node.js API).
const isNodeJS = global.process && global.process.title === "node";
if (isNodeJS) {
global.require = require;
global.fs = require("fs");
const nodeCrypto = require("crypto");
global.crypto = {
getRandomValues(b) {
nodeCrypto.randomFillSync(b);
},
};
global.performance = {
now() {
const [sec, nsec] = process.hrtime();
return sec * 1000 + nsec / 1000000;
},
};
const util = require("util");
global.TextEncoder = util.TextEncoder;
global.TextDecoder = util.TextDecoder;
} else {
let outputBuf = "";
global.fs = {
constants: { O_WRONLY: -1, O_RDWR: -1, O_CREAT: -1, O_TRUNC: -1, O_APPEND: -1, O_EXCL: -1 }, // unused
writeSync(fd, buf) {
outputBuf += decoder.decode(buf);
const nl = outputBuf.lastIndexOf("\n");
if (nl != -1) {
console.log(outputBuf.substr(0, nl));
outputBuf = outputBuf.substr(nl + 1);
}
return buf.length;
},
write(fd, buf, offset, length, position, callback) {
if (offset !== 0 || length !== buf.length || position !== null) {
throw new Error("not implemented");
}
const n = this.writeSync(fd, buf);
callback(null, n);
},
open(path, flags, mode, callback) {
const err = new Error("not implemented");
err.code = "ENOSYS";
callback(err);
},
read(fd, buffer, offset, length, position, callback) {
const err = new Error("not implemented");
err.code = "ENOSYS";
callback(err);
},
fsync(fd, callback) {
callback(null);
},
};
}
const encoder = new TextEncoder("utf-8");
const decoder = new TextDecoder("utf-8");
global.Go = class {
constructor() {
this.argv = ["js"];
this.env = {};
this.exit = (code) => {
if (code !== 0) {
console.warn("exit code:", code);
}
};
this._exitPromise = new Promise((resolve) => {
this._resolveExitPromise = resolve;
});
this._pendingEvent = null;
this._scheduledTimeouts = new Map();
this._nextCallbackTimeoutID = 1;
const mem = () => {
// The buffer may change when requesting more memory.
return new DataView(this._inst.exports.mem.buffer);
}
const setInt64 = (addr, v) => {
mem().setUint32(addr + 0, v, true);
mem().setUint32(addr + 4, Math.floor(v / 4294967296), true);
}
const getInt64 = (addr) => {
const low = mem().getUint32(addr + 0, true);
const high = mem().getInt32(addr + 4, true);
return low + high * 4294967296;
}
const loadValue = (addr) => {
const f = mem().getFloat64(addr, true);
if (f === 0) {
return undefined;
}
if (!isNaN(f)) {
return f;
}
const id = mem().getUint32(addr, true);
return this._values[id];
}
const storeValue = (addr, v) => {
const nanHead = 0x7FF80000;
if (typeof v === "number") {
if (isNaN(v)) {
mem().setUint32(addr + 4, nanHead, true);
mem().setUint32(addr, 0, true);
return;
}
if (v === 0) {
mem().setUint32(addr + 4, nanHead, true);
mem().setUint32(addr, 1, true);
return;
}
mem().setFloat64(addr, v, true);
return;
}
switch (v) {
case undefined:
mem().setFloat64(addr, 0, true);
return;
case null:
mem().setUint32(addr + 4, nanHead, true);
mem().setUint32(addr, 2, true);
return;
case true:
mem().setUint32(addr + 4, nanHead, true);
mem().setUint32(addr, 3, true);
return;
case false:
mem().setUint32(addr + 4, nanHead, true);
mem().setUint32(addr, 4, true);
return;
}
let ref = this._refs.get(v);
if (ref === undefined) {
ref = this._values.length;
this._values.push(v);
this._refs.set(v, ref);
}
let typeFlag = 0;
switch (typeof v) {
case "string":
typeFlag = 1;
break;
case "symbol":
typeFlag = 2;
break;
case "function":
typeFlag = 3;
break;
}
mem().setUint32(addr + 4, nanHead | typeFlag, true);
mem().setUint32(addr, ref, true);
}
const loadSlice = (addr) => {
const array = getInt64(addr + 0);
const len = getInt64(addr + 8);
return new Uint8Array(this._inst.exports.mem.buffer, array, len);
}
const loadSliceOfValues = (addr) => {
const array = getInt64(addr + 0);
const len = getInt64(addr + 8);
const a = new Array(len);
for (let i = 0; i < len; i++) {
a[i] = loadValue(array + i * 8);
}
return a;
}
const loadString = (addr) => {
const saddr = getInt64(addr + 0);
const len = getInt64(addr + 8);
return decoder.decode(new DataView(this._inst.exports.mem.buffer, saddr, len));
}
const timeOrigin = Date.now() - performance.now();
this.importObject = {
go: {
// Go's SP does not change as long as no Go code is running. Some operations (e.g. calls, getters and setters)
// may synchronously trigger a Go event handler. This makes Go code get executed in the middle of the imported
// function. A goroutine can switch to a new stack if the current stack is too small (see morestack function).
// This changes the SP, thus we have to update the SP used by the imported function.
// func wasmExit(code int32)
"runtime.wasmExit": (sp) => {
const code = mem().getInt32(sp + 8, true);
this.exited = true;
delete this._inst;
delete this._values;
delete this._refs;
this.exit(code);
},
// func wasmWrite(fd uintptr, p unsafe.Pointer, n int32)
"runtime.wasmWrite": (sp) => {
const fd = getInt64(sp + 8);
const p = getInt64(sp + 16);
const n = mem().getInt32(sp + 24, true);
fs.writeSync(fd, new Uint8Array(this._inst.exports.mem.buffer, p, n));
},
// func nanotime() int64
"runtime.nanotime": (sp) => {
setInt64(sp + 8, (timeOrigin + performance.now()) * 1000000);
},
// func walltime() (sec int64, nsec int32)
"runtime.walltime": (sp) => {
const msec = (new Date).getTime();
setInt64(sp + 8, msec / 1000);
mem().setInt32(sp + 16, (msec % 1000) * 1000000, true);
},
// func scheduleTimeoutEvent(delay int64) int32
"runtime.scheduleTimeoutEvent": (sp) => {
const id = this._nextCallbackTimeoutID;
this._nextCallbackTimeoutID++;
this._scheduledTimeouts.set(id, setTimeout(
() => { this._resume(); },
getInt64(sp + 8) + 1, // setTimeout has been seen to fire up to 1 millisecond early
));
mem().setInt32(sp + 16, id, true);
},
// func clearTimeoutEvent(id int32)
"runtime.clearTimeoutEvent": (sp) => {
const id = mem().getInt32(sp + 8, true);
clearTimeout(this._scheduledTimeouts.get(id));
this._scheduledTimeouts.delete(id);
},
// func getRandomData(r []byte)
"runtime.getRandomData": (sp) => {
crypto.getRandomValues(loadSlice(sp + 8));
},
// func stringVal(value string) ref
"syscall/js.stringVal": (sp) => {
storeValue(sp + 24, loadString(sp + 8));
},
// func valueGet(v ref, p string) ref
"syscall/js.valueGet": (sp) => {
const result = Reflect.get(loadValue(sp + 8), loadString(sp + 16));
sp = this._inst.exports.getsp(); // see comment above
storeValue(sp + 32, result);
},
// func valueSet(v ref, p string, x ref)
"syscall/js.valueSet": (sp) => {
Reflect.set(loadValue(sp + 8), loadString(sp + 16), loadValue(sp + 32));
},
// func valueIndex(v ref, i int) ref
"syscall/js.valueIndex": (sp) => {
storeValue(sp + 24, Reflect.get(loadValue(sp + 8), getInt64(sp + 16)));
},
// valueSetIndex(v ref, i int, x ref)
"syscall/js.valueSetIndex": (sp) => {
Reflect.set(loadValue(sp + 8), getInt64(sp + 16), loadValue(sp + 24));
},
// func valueCall(v ref, m string, args []ref) (ref, bool)
"syscall/js.valueCall": (sp) => {
try {
const v = loadValue(sp + 8);
const m = Reflect.get(v, loadString(sp + 16));
const args = loadSliceOfValues(sp + 32);
const result = Reflect.apply(m, v, args);
sp = this._inst.exports.getsp(); // see comment above
storeValue(sp + 56, result);
mem().setUint8(sp + 64, 1);
} catch (err) {
storeValue(sp + 56, err);
mem().setUint8(sp + 64, 0);
}
},
// func valueInvoke(v ref, args []ref) (ref, bool)
"syscall/js.valueInvoke": (sp) => {
try {
const v = loadValue(sp + 8);
const args = loadSliceOfValues(sp + 16);
const result = Reflect.apply(v, undefined, args);
sp = this._inst.exports.getsp(); // see comment above
storeValue(sp + 40, result);
mem().setUint8(sp + 48, 1);
} catch (err) {
storeValue(sp + 40, err);
mem().setUint8(sp + 48, 0);
}
},
// func valueNew(v ref, args []ref) (ref, bool)
"syscall/js.valueNew": (sp) => {
try {
const v = loadValue(sp + 8);
const args = loadSliceOfValues(sp + 16);
const result = Reflect.construct(v, args);
sp = this._inst.exports.getsp(); // see comment above
storeValue(sp + 40, result);
mem().setUint8(sp + 48, 1);
} catch (err) {
storeValue(sp + 40, err);
mem().setUint8(sp + 48, 0);
}
},
// func valueLength(v ref) int
"syscall/js.valueLength": (sp) => {
setInt64(sp + 16, parseInt(loadValue(sp + 8).length));
},
// valuePrepareString(v ref) (ref, int)
"syscall/js.valuePrepareString": (sp) => {
const str = encoder.encode(String(loadValue(sp + 8)));
storeValue(sp + 16, str);
setInt64(sp + 24, str.length);
},
// valueLoadString(v ref, b []byte)
"syscall/js.valueLoadString": (sp) => {
const str = loadValue(sp + 8);
loadSlice(sp + 16).set(str);
},
// func valueInstanceOf(v ref, t ref) bool
"syscall/js.valueInstanceOf": (sp) => {
mem().setUint8(sp + 24, loadValue(sp + 8) instanceof loadValue(sp + 16));
},
"debug": (value) => {
console.log(value);
},
}
};
}
async run(instance) {
this._inst = instance;
this._values = [ // TODO: garbage collection
NaN,
0,
null,
true,
false,
global,
this._inst.exports.mem,
this,
];
this._refs = new Map();
this.exited = false;
const mem = new DataView(this._inst.exports.mem.buffer)
// Pass command line arguments and environment variables to WebAssembly by writing them to the linear memory.
let offset = 4096;
const strPtr = (str) => {
let ptr = offset;
new Uint8Array(mem.buffer, offset, str.length + 1).set(encoder.encode(str + "\0"));
offset += str.length + (8 - (str.length % 8));
return ptr;
};
const argc = this.argv.length;
const argvPtrs = [];
this.argv.forEach((arg) => {
argvPtrs.push(strPtr(arg));
});
const keys = Object.keys(this.env).sort();
argvPtrs.push(keys.length);
keys.forEach((key) => {
argvPtrs.push(strPtr(`${key}=${this.env[key]}`));
});
const argv = offset;
argvPtrs.forEach((ptr) => {
mem.setUint32(offset, ptr, true);
mem.setUint32(offset + 4, 0, true);
offset += 8;
});
this._inst.exports.run(argc, argv);
if (this.exited) {
this._resolveExitPromise();
}
await this._exitPromise;
}
_resume() {
if (this.exited) {
throw new Error("Go program has already exited");
}
this._inst.exports.resume();
if (this.exited) {
this._resolveExitPromise();
}
}
_makeFuncWrapper(id) {
const go = this;
return function () {
const event = { id: id, this: this, args: arguments };
go._pendingEvent = event;
go._resume();
return event.result;
};
}
}
if (isNodeJS) {
if (process.argv.length < 3) {
process.stderr.write("usage: go_js_wasm_exec [wasm binary] [arguments]\n");
process.exit(1);
}
const go = new Go();
go.argv = process.argv.slice(2);
go.env = Object.assign({ TMPDIR: require("os").tmpdir() }, process.env);
go.exit = process.exit;
WebAssembly.instantiate(fs.readFileSync(process.argv[2]), go.importObject).then((result) => {
process.on("exit", (code) => { // Node.js exits if no event handler is pending
if (code === 0 && !go.exited) {
// deadlock, make Go print error and stack traces
go._pendingEvent = { id: 0 };
go._resume();
}
});
return go.run(result.instance);
}).catch((err) => {
throw err;
});
}
})();
runtime.resetMemoryDataView() function is part of wasm_exec.js support script that bridges WebAssembly binary with JavaScript environment. This and similar errors often mean that wasm_exec.js isn't compatible with WebAssembly binary because version of Golang used to compile binary is different (usually newer) than one wasm_exec.js was taken from.
When running or shipping Golang WebAssembly binary always make sure that you are using wasm_exec.js support script from the same version of Golang as was used to compile binary. You can copy it from $(go env GOROOT)/misc/wasm/wasm_exec.js to be sure.
See official Golang WebAssembly wiki for further details.
As blami suggested a simple:
cp $(go env GOROOT)/misc/wasm/wasm_exec.js ./path/to/old/wasm_exec.js
Worked for me.

Use of take in drop function of persisted list

I am reading Learning Rust With Entirely Too Many Linked Lists, specifically the chapter about a persistent singly-linked stack.
I am having difficulty understanding use of take in the drop function.
Without it, I get the same logs in drop.
use std::rc::Rc;
use std::fmt;
type Link<T> = Option<Rc<Node<T>>>;
#[derive(Debug)]
pub struct List<T>
where
T: fmt::Debug,
{
head: Link<T>,
}
#[derive(Debug)]
struct Node<T>
where
T: fmt::Debug,
{
val: T,
next: Link<T>,
}
impl<T> List<T>
where
T: fmt::Debug,
{
pub fn new() -> Self {
List { head: None }
}
pub fn append(&self, val: T) -> List<T> {
List {
head: Some(Rc::new(Node {
val: val,
next: self.head.clone(),
})),
}
}
}
impl<T> Drop for List<T>
where
T: fmt::Debug,
{
fn drop(&mut self) {
println!("1 : {:?}", self);
let mut curr_list = self.head.take();
println!("2");
while let Some(node) = curr_list {
println!("3");
match Rc::try_unwrap(node) {
Err(_) => {
println!("4");
break;
}
Ok(ref mut x) => {
println!("5");
curr_list = x.next.take()
}
}
println!("6");
}
println!("7");
}
}
fn main() {
let list = List::new().append(1).append(2);
let _list2 = list.append(3);
let _list3 = list.append(3);
let _list4 = list.append(3);
let _list5 = list.append(3);
}
Playground
It outputs:
1 : List { head: Some(Node { val: 1, next: None }) }
2
3
4 : stopping at 1
7
1 : List { head: None }
2
7
1 : List { head: Some(Node { val: 3, next: Some(Node { val: 2, next: Some(Node { val: 1, next: None }) }) }) }
2
3
5: dropping 3
6
3
4 : stopping at 2
7
1 : List { head: Some(Node { val: 3, next: Some(Node { val: 2, next: Some(Node { val: 1, next: None }) }) }) }
2
3
5: dropping 3
6
3
4 : stopping at 2
7
1 : List { head: Some(Node { val: 3, next: Some(Node { val: 2, next: Some(Node { val: 1, next: None }) }) }) }
2
3
5: dropping 3
6
3
4 : stopping at 2
7
1 : List { head: Some(Node { val: 3, next: Some(Node { val: 2, next: Some(Node { val: 1, next: None }) }) }) }
2
3
5: dropping 3
6
3
4 : stopping at 2
7
1 : List { head: Some(Node { val: 2, next: Some(Node { val: 1, next: None }) }) }
2
3
5: dropping 2
6
3
5: dropping 1
6
7
The first and second 1 : List { is because of first line in main, which is destroying temp lists created. The next 3 1 : List { are lists getting dropped in reverse order of creation. Finally the shared list of size 2 is dropped.
Even if I remove take from line : curr_list = x.next.take() and remove ref mut for x in fn drop I still get the same output.
What is x there and why does take() have no effects?
I earlier thought that take would change the original list by setting the next to None, but that is not possible as that will invalidate the list for other people who have references.
Implementing drop on Node :
impl<T> Drop for Node<T> where T: fmt::Debug {
fn drop(&mut self) {
println!("Node drop : {:?}", self.val);
}
}
so, Just after Ok(ref mut x) block, Node x is dropped.

Resources