question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Return array value

See original GitHub issue

Feature Request

Overview

Not sure if this is a bug or not, I read through the docs and found no reference to this. Also tried looking through issues.

I can’t return an array. Correct me if i’m off here, but I think it’s because behind the scenes, walt is just grabbing the memory reference for the first element of the array. Similar to C if you print str (where str is an char[]), you’d just get the first char.

This is different from the issue about strings.

Impact

Large (if it doesn’t already exist).

Details

Here’s a dumb little example I tried:

const memory: Memory<{ initial: 1}>;

export function sortItems(array: i32[]): i32[] {
  let i: i32;
  let toReturn: i32[] = 0;
  for (i = 0; i < 5; i += 1) {
    toReturn[i] = i;
  }
  return toReturn;
}

I get Inconsistent return value. Expected i32[], received i32

Due Date

¯\(ツ)/¯ 7/20/18 if I missed something, I guess there is no due date.

I’m working on this for a hackday (which is only today), so either way, I’ll unfortunately have to ditch this project. I’m going to just use the C -> WASM compiler. However, I opened this issue because I’d like to experiment more in the future.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
njtrettelcommented, Jul 6, 2018

Okay, sorry for the long thread of questions. Now I’ve got the memory object imported from env in the walt code, and I have created a DataView with the buffer to read from. But whenever I try to do array operations in walt, I get an out of bounds error. I think I’m just missing something again.

~Apologies if this is documented. I searched for it and couldn’t find it here. The WebAssembly docs don’t help much in this case.~

EDIT: It sure is documented, I just missed the part that initial actual means the size 😄

@ballercat this is an awesome library, thanks for putting it together.

0reactions
ballercatcommented, Jul 6, 2018

Sure, all I mean is that it needs to be part of the imports to the WASM module. Per wasm spec, a Memory reference in a WebAssembly module is either declared or imported as a regular import with a memory type.

const memory = new WebAssembly.Memory({ initial: 1 });
makeSorter({
  env: {
    memory,
    log: console.log
  }
}).then(module => ...);

walt source, but really anything that compiles to wasm

import { memory: Memory<{ initial: 1 }> } from 'env';

MDN reference is a good source of info https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Memory

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Return an Array in Java - Javatpoint
Example 1 · import java.util.Arrays; · public class ReturnArrayExample1 · { · public static void main(String args[]) · { · int[] a=numbers(); //obtain...
Read more >
Array.prototype.values() - JavaScript - MDN Web Docs
The values() method returns a new array iterator object that iterates the value of each index in the array.
Read more >
How to Return an Array in Java - Studytonight
In a program given below, we created a method public static int[] getArray() which will return an array arr that assigned to arr...
Read more >
Return array from function in C - Tutorialspoint
Return array from function in C, C programming does not allow to return an entire array as an argument to a function. However,...
Read more >
How to return a specific element of an array? - Stack Overflow
It's not at all clear what you want. You said you want one element, but you also want odd "numbers" plural and have...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found