Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document how to export a function #281

Open
onedayitwillmake opened this issue Dec 22, 2021 · 2 comments
Open

Document how to export a function #281

onedayitwillmake opened this issue Dec 22, 2021 · 2 comments
Labels
documentation Improvements or additions to documentation good first issue Good for newcomers

Comments

@onedayitwillmake
Copy link

From reading the docs, it does not appear possible to pass an extra -Xlinker flag when running carton bundle or carton dev

The SwiftWASM book which encourages users to install and Carton, does not mention it when it describes how to export a function: https://book.swiftwasm.org/examples/exporting-function.html

@augustsaintfreytag
Copy link

Jumping in from other implications to this question, I found that even when functions are exported and available at instance.exports[name], the functions can not be called and lead to an “Unreachable executed” error in my case. Executing the same code through wasi.start(…) works fine.

Is adding linker flags (i.e. -Xlinker -export-dynamic) really the only issue on your end?

@onedayitwillmake
Copy link
Author

onedayitwillmake commented Jan 19, 2022

Answering my own above question, and leaving here for posterity I found that setting the linker flags in the Package.swift allowed Carton to pick them up and export them properly. It's also more ergonomic than carrying around the typo prone string of flags.

Example of projects Package.swift

// swift-tools-version:5.3

import PackageDescription

let package = Package(
  name: "my-project",
  platforms: [
    .macOS(.v11)
  ],
  products: [
    .executable(
      name: "MyProjectWASMWeb",
      targets: ["MyProjectWASMWeb"])
  ],
  dependencies: [
    .package(name: "JavaScriptKit", url: "https://github.com/swiftwasm/JavaScriptKit", from: "0.10.1")
  ],
  targets: [ 
    .target(
      name: "MyProjectWASM",
      dependencies: [
        .product(name: "JavaScriptKit", package: "JavaScriptKit")
      ],
      linkerSettings: [
        .unsafeFlags(
          [
            "-Xlinker", "--export=myFunction",
            "-Xlinker", "--export=myOtherFunction",
             // Had to add the `Xlinker` flags below to work around compilation issue
            "-Xlinker", "-licuuc",
            "-Xlinker", "-licui18n",
            "-Xlinker", "-lCoreFoundation",
          ],
          .when(platforms: [.wasi])
        )
      ]
    ),
    .target(
      name: "MyProjectWASMWeb",
      dependencies: [
        "MyProjectWASM",
        .product(name: "JavaScriptKit", package: "JavaScriptKit")
      ]),
    .testTarget(
      name: "MyProjectWASMTests",
      dependencies: ["MyProjectWASM"]),
  ]
)

@MaxDesiatov MaxDesiatov added documentation Improvements or additions to documentation good first issue Good for newcomers labels Feb 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants