dotnet / vblang

The home for design of the Visual Basic .NET programming language and runtime library.
290 stars 64 forks source link

WebAssembly Compiler for VisualBasic.NET #406

Open xieguigang opened 5 years ago

xieguigang commented 5 years ago

Hey there,

An alpha version of WebAssembly compiler for VisualBasic.NET language was release, visit page v0.5.136-alpha and have fun!

WebAssembly for VisualBasic.NET

FAQ

1. Which platform that I can running my VisualBasic.NET application

Theoretically, you can develop your VisualBasic.NET application for Windows/Linux/Mac/IOS/Android, from PC, mobile phone, tablet and Raspberry Pi, running on any platform if the platform supports WebAssembly!

2. How fast that my VisualBasic.NET can running

Theoretically, your VisualBasic.NET application is running as fast as C/C++ application, as VisualBasic.NET is generate same assembly code as C/C++ application it does.

3. Can I run my VisualBasic.NET application on nodejs environment

yes! If your current nodejs environment supports WebAssembly.

4. Can I publish my VisualBasic.NET application onto npm

yes! If the npm supports WebAssembly package.

5. Win32/.NET Framework api supports?

no! Only supports the api that javascript expose to your VisualBasic.NET application. So .NET Framework is no longer available if you target your VisualBasic.NET application to WebAssembly platform. You are writing a very Core version of VB.NET application, which means almost all of the things must create from scratch if you target your VB.NET application to WebAssembly platform.

6. What can I do with WebAssembly

WebAssembly is a kind of solution for aimed at high-density computing on javascript environment, so the best choice is HTML5 Game development using VisualBasic.NET with Unity, or Edge computing for your business. And of course you can develop any application for every platform, so on.

Using WebAssembly compiler for VB.NET

For using WebAssembly compiler for VB.NET language, please follow these steps:

1. Write your applications' code

Write your VisualBasic.NET application in VisualStudio, and then save it.

Module Demo

    Declare Function Print Lib "console" Alias "log" (text As String) As Integer

    Public Function HelloWorld() As Integer 
        Call Print("Hello World!")
        Return 0
    End Function

End Module

Project only supports a single source file, a single module, Class/Structure/Enum/Array is not supports yet in current version of the VB.NET wasm compiler, but all of this VB.NET programming feature will be supportted in future works. Probably not supports generic method.

2. Run wasm compiler

Current version VB.NET wasm compiler just supports compile a single source file. A VB.NET project with multiple source file will be supported in the future. For compile a VB.NET application, create a console application, and add reference to VisualBasic.Wasm.dll, at last write these code for compile your VisualBasic source file:

Dim vb = "/path/to/app.vb"
Dim [module] = Wasm.CreateModule(vb)

' Target Assembly file (*.wasm)
[module].Compile("HelloWorld.wasm")
' Compiler debug used only (*.wast, *.dmp)
[module].ToSExpression.SaveTo("HelloWorld.wast")
[module].HexDump(True).SaveTo("HelloWorld.dmp")

Compiler project source file available at: https://github.com/GCModeller-Cloud/data.ts/tree/878d93e4d011f666d0dc4dbc0062a6fd1685bc9b/TsBuild/Wasm

3. Run VB.NET application from javascript environment

Add script reference to visualbasic.wasm.js, and then running VB.NET application on web browser or nodejs from a javascript loader like:

let assmUrl = "vbscript/HelloWorld.wasm";

TypeScript.Wasm.RunAssembly(assmUrl, {
    // run app from a public method which its name is
    // RunApp or something else
    run: VB => VB.instance.exports.RunApp(),
    // some build-in javascript api that expose to VB.NET application
    api: { document: true, console: true },
    // Other javascript api expose to VB.NET application, like jquery, bootstrap, etc
    imports: {}
});

hello_world

Have fun!

rskar-git commented 5 years ago

Wow! this looks cool, can hardly wait to try this out!

ericmutta commented 5 years ago

I am following WebAssembly very closely (I have a reminder on my phone to check its progress every few weeks) so I am super excited to see someone working on it from the VB perspective :+1: :+1:

My hope for WebAssembly is that it will make it easier to run the Android app I have spent the past three years making (and going practically insane in the process) on other platforms like iOS via the browser.

Hopefully within a year or two we will have a WebAssembly run-time that supports .NET Standard 2.0. Awesome work @xieguigang!

VBAndCs commented 5 years ago

@xieguigang Amazing! I hope we can use this with vbxml and ZML pages

Nukepayload2 commented 5 years ago

Awesome! It's a nice addition to Blazor.

Feature/Attribute Blazor Your compiler
File size Huge, larger than 5 MB Small
Supports JS API True True
Supports Core VB syntax True Partial
Supports .NET Standard 2.0 API True False
rrvenki commented 5 years ago

@Nukepayload2 Kindly let us know the file size (value) for this compiler in comparison to Blazor. That helps to think of hosting this in many microcontrollers which has .NET Core or Micro... and open new avenues.

cristianlt23 commented 3 years ago

Fantastic!!!