GithubHelp home page GithubHelp logo

Comments (8)

jedisct1 avatar jedisct1 commented on May 31, 2024

Maybe @MaxGraey would be the one to ask here :)

from wasi-experimental-http.

MaxGraey avatar MaxGraey commented on May 31, 2024

сс @dcodeIO

from wasi-experimental-http.

dcodeIO avatar dcodeIO commented on May 31, 2024

Finalization should happen eventually, once the GC kicks in. With the incremental runtime, when this happens depends, though. With default settings, the first threshold is at about half the memory used for example, so depending on the program's allocation pattern, the GC may kick in early, or even never. Subsequent GCs happen when used memory doubles again, relative to remaining memory used after the previous collection. Probably not good enough to rely upon for closing handles.

from wasi-experimental-http.

radu-matei avatar radu-matei commented on May 31, 2024

Thanks a lot for your reply, @dcodeIO!

I guess we have to keep the requirement that consumers have to manually close the handle for now, until we have a better solution, such as a destructor.

from wasi-experimental-http.

jedisct1 avatar jedisct1 commented on May 31, 2024

That issue is common to virtual alll WASI APIs, that heavily depend on handles.

In Zig, instead of finalizers, we have defer and errdefer statements that can automatically free a resource before a function returns, or on error.

Maybe this is something that could also be implemented in AssemblyScript?

from wasi-experimental-http.

MaxGraey avatar MaxGraey commented on May 31, 2024

defer is just early declaration for releasing resource (explicit RAII) and this the same as manually close such resource before return but with some more continent way. So

fn foo() -> bool {
  let res = io.open(path)
  defer(res)
  // some code
  if (fail) return false // will implicitly call io.close(res) before return. Injected in compile time
  // some other code
  return true // will implicitly call io.close(res) before return
}

and it's the same as:

fn foo() -> bool {
  let res = io.open(path)
  // some code
  if (fail) {
    io.close(res)
    return false
  }
  // some other code
  io.close(res)
  return true
}

and this works only inside user space

from wasi-experimental-http.

MaxGraey avatar MaxGraey commented on May 31, 2024

JavaScript / Typescript usually return callback function which contine code for finalization:

export function foo(): () => bool {
  const desc = fs.open(path);
  // some code
  return () => {
     fs.close(desc);
     return true;
  };
}

while AS don't support closures. It may be temporary workaround:

let tmpDesc: FileDesc | null = null; // global var

export function foo(): () => bool {
  tmpDesc = fs.open(path); // store to global desc
  // some code
  return () => {
     fs.close(tmpDesc); // reat from global desc
     return true;
  };
}

from wasi-experimental-http.

MaxGraey avatar MaxGraey commented on May 31, 2024

If you required many descriptors simultaneously it may be something like this:

let descMap: Map<i32, FileDesc> = new Map; // it could be stack / queue
let gId = 0;

export function foo(): (id: i32) => bool {
  descMap.set(gId++, fs.open(path)); // store to global desc
  // some code
  return (id: i32) => {
     if (descMap.has(id)) {
       let desc = descMap.get(id);
       fs.close(desc); // reat from global desc
       descMap.delete(id);
       return true;
     }
     return false;
  };
}

from wasi-experimental-http.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.