typestar

Imports en WebAssembly

El módulo declara sus necesidades; el host las provee o no hay trato.

(module
  ;; Imports are the module's stated needs: the host supplies this
  ;; function at instantiation or the module refuses to start.
  (import "env" "log_i32" (func $log (param i32)))

  (global $reports (mut i32) (i32.const 0))

  (func (export "report") (param $reading i32)
    (global.set $reports
      (i32.add (global.get $reports) (i32.const 1)))
    (call $log (local.get $reading))
    (call $log (global.get $reports))))

Cómo funciona

  1. (import "env" "log_i32" (func $log ...)) es un nombre de dos niveles.
  2. El import se ata a $log y se llama como cualquier función local.
  3. La instanciación falla de plano si el host no ofrece nada que calce.

Palabras clave y builtins usados aquí

El intento, en números

Líneas
12
Caracteres a escribir
408
Tokens
79
Ritmo de tres estrellas
65 tpm

Al ritmo de tres estrellas de 65 tokens por minuto, este intento toma unos 73 segundos.

Escribe este fragmento

Paso 3 de 3 en Globales y arranque; paso 22 de 28 en Fundamentos del lenguaje.

← Anterior Siguiente →