apollovm 0.0.48 copy "apollovm: ^0.0.48" to clipboard
apollovm: ^0.0.48 copied to clipboard

ApolloVM, a Multilingual portable VM (native, JS/Web, Flutter) for Dart, Java, JavaScript with on-the-fly Wasm compilation.

example/apollovm_example.dart

import 'package:apollovm/apollovm.dart';

void main() async {
  var vm = ApolloVM();

  var codeUnit = SourceCodeUnit(
      'dart',
      r'''
      
      class Foo {
      
          int main(List<Object> args) {
            var title = args[0];
            var a = args[1];
            var b = args[2] ~/ 2;
            var c = args[3] * 3;
            
            if (c > 120) {
              c = 120 ;
            }
            
            var str = 'variables> a: $a ; b: $b ; c: $c' ;
            var sumAB = a + b ;
            var sumABC = a + b + c;
            
            print(str);
            print(title);
            print(sumAB);
            print(sumABC);
            
            // Map:
            var map = <String,int>{
            'a': a,
            'b': b,
            'c': c,
            'sumAB': sumAB,
            "sumABC": sumABC,
            };
            
            print('Map: $map');
            print('Map `b`: ${map['b']}');
            
            return map['sumABC'];
          }
          
      }
      
      ''',
      id: 'test');

  var loadOK = await vm.loadCodeUnit(codeUnit);

  if (!loadOK) {
    print("Can't load source!");
    return;
  }

  print('---------------------------------------');

  var dartRunner = vm.createRunner('dart')!;

  // Map the `print` function in the VM:
  dartRunner.externalPrintFunction = (o) => print("» $o");

  var astValue = await dartRunner.executeClassMethod(
    '',
    'Foo',
    'main',
    positionalParameters: [
      ['Sums:', 10, 30, 50]
    ],
  );

  var result = astValue.getValueNoContext();
  print('Result: $result');

  print('---------------------------------------');

  // Regenerate code in Dart:
  var codeStorageDart = vm.generateAllCodeIn('dart');
  var allSourcesDart = codeStorageDart.writeAllSources();
  print(allSourcesDart);

  print('---------------------------------------');

  // Regenerate code in Java11:
  var codeStorageJava = vm.generateAllCodeIn('java11');
  var allSourcesJava = codeStorageJava.writeAllSources();
  print(allSourcesJava);
}

/////////////
// OUTPUT: //
/////////////
// ---------------------------------------
// » variables> a: 10 ; b: 15 ; c: 120
// » Sums:
// » 25
// » 145
// » Map: {a: 10, b: 15, c: 120, sumAB: 25, sumABC: 145}
// » Map `b`: 15
// Result: 145
// ---------------------------------------
// <<<< [SOURCES_BEGIN] >>>>
// <<<< NAMESPACE="" >>>>
// <<<< CODE_UNIT_START="/test" >>>>
// class Foo {
//
//   int main(List<Object> args) {
//     var title = args[0];
//     var a = args[1];
//     var b = args[2] ~/ 2;
//     var c = args[3] * 3;
//     if (c > 120) {
//         c = 120;
//     }
//
//     var str = 'variables> a: $a ; b: $b ; c: $c';
//     var sumAB = a + b;
//     var sumABC = a + b + c;
//     print(str);
//     print(title);
//     print(sumAB);
//     print(sumABC);
//     var map = <String,int>{'a': a, 'b': b, 'c': c, 'sumAB': sumAB, 'sumABC': sumABC};
//     print('Map: $map');
//     print('Map `b`: ${map['b']}');
//     return map['sumABC'];
//   }
//
// }
// <<<< CODE_UNIT_END="/test" >>>>
// <<<< [SOURCES_END] >>>>
//
// ---------------------------------------
// <<<< [SOURCES_BEGIN] >>>>
// <<<< NAMESPACE="" >>>>
// <<<< CODE_UNIT_START="/test" >>>>
// class Foo {
//
//   int main(Object[] args) {
//     var title = args[0];
//     var a = args[1];
//     var b = args[2] / 2;
//     var c = args[3] * 3;
//     if (c > 120) {
//         c = 120;
//     }
//
//     var str = "variables> a: " + a + " ; b: " + b + " ; c: " + c;
//     var sumAB = a + b;
//     var sumABC = a + b + c;
//     print(str);
//     print(title);
//     print(sumAB);
//     print(sumABC);
//     var map = new HashMap<String,int>(){{
//       put("a", a);
//       put("b", b);
//       put("c", c);
//       put("sumAB", sumAB);
//       put("sumABC", sumABC);
//     }};
//     print("Map: " + map);
//     print("Map `b`: " + String.valueOf( map["b"] ));
//     return map["sumABC"];
//   }
//
// }
// <<<< CODE_UNIT_END="/test" >>>>
// <<<< [SOURCES_END] >>>>
//
42
likes
0
pub points
31%
popularity

Publisher

unverified uploader

ApolloVM, a Multilingual portable VM (native, JS/Web, Flutter) for Dart, Java, JavaScript with on-the-fly Wasm compilation.

Repository (GitHub)
View/report issues

Topics

#vm #dart #java #compiler #translator

License

unknown (license)

Dependencies

args, async_extension, collection, data_serializer, petitparser, swiss_knife, wasm_interop

More

Packages that depend on apollovm