Skip to content

Commit 8e85fe6

Browse files
authored
chore: Add runtime examples (#24)
* add pretty node example * re-enable tests
1 parent 1948d95 commit 8e85fe6

File tree

12 files changed

+2910
-18
lines changed

12 files changed

+2910
-18
lines changed

.github/workflows/runtime.yaml

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,26 @@ on:
99
pull_request:
1010

1111
jobs:
12-
# Test:
13-
# runs-on: ubuntu-22.04
14-
# steps:
15-
# - uses: actions/checkout@v4
16-
# - uses: dart-lang/setup-dart@v1
17-
# - uses: houseabsolute/actions-rust-cross@v1
18-
# with:
19-
# command: build
20-
# target: x86_64-unknown-linux-gnu
21-
# args: "--locked --release"
22-
# strip: true
23-
24-
# - name: Run Dart Tests
25-
# run: |
26-
# mkdir -p ~/.globe/runtime && cd ~/.globe/runtime
27-
# mv target/x86_64-unknown-linux-gnu/release/libglobe_runtime.so ~/.globe/runtime
28-
# cd packages/globe_runtime
29-
# dart test --reporter expanded --concurrency=1
12+
Test:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v4
16+
- uses: dart-lang/setup-dart@v1
17+
18+
- name: Install Globe Runtime
19+
run: |
20+
sudo apt-get update && sudo apt-get install -y jq curl
21+
mkdir -p ~/.globe/runtime && cd ~/.globe/runtime
22+
curl -s https://api.github.com/repos/invertase/globe_runtime/releases/latest \
23+
| jq -r '.assets[] | select(.name == "libglobe_runtime-x86_64-unknown-linux-gnu.so") | .browser_download_url' \
24+
| xargs curl -L -OJ
25+
26+
- name: Run Dart Tests
27+
working-directory: packages/globe_runtime
28+
run: dart test --reporter expanded --concurrency=1
3029

3130
Build:
31+
needs: Test
3232
name: Build - ${{ matrix.platform.os-name }}
3333
strategy:
3434
matrix:

examples/pretty_node/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist

examples/pretty_node/README.md

Whitespace-only changes.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import 'dart:async';
2+
import 'dart:convert';
3+
import 'dart:io';
4+
5+
import 'package:globe_runtime/globe_runtime.dart';
6+
import 'package:pretty_node/pretty_node_source.dart';
7+
8+
const moduleName = 'PrettyNode';
9+
10+
Future<String> callJsFunction(
11+
String functionName, {
12+
List<FFIConvertible> args = const [],
13+
}) async {
14+
final completer = Completer<String>();
15+
GlobeRuntime.instance.callFunction(
16+
moduleName,
17+
function: functionName,
18+
args: args,
19+
onData: (data) {
20+
if (data.hasError()) {
21+
completer.completeError(data.error);
22+
} else {
23+
completer.complete(utf8.decode(data.data));
24+
}
25+
return true;
26+
},
27+
);
28+
return completer.future;
29+
}
30+
31+
void main() async {
32+
await GlobeRuntime.instance.registerModule(moduleName, packageSource);
33+
34+
final prettyBytes = await callJsFunction('make_pretty_bytes', args: [
35+
569943344333884.toFFIType,
36+
]);
37+
stdout.writeln('Pretty Bytes: $prettyBytes');
38+
39+
final prettyMs = await callJsFunction('make_pretty_ms', args: [
40+
8003333.toFFIType,
41+
]);
42+
stdout.writeln('Pretty MS: $prettyMs');
43+
44+
GlobeRuntime.instance.dispose();
45+
46+
exit(0);
47+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import pretty_bytes from "pretty-bytes";
2+
import pretty_ms from "pretty-ms";
3+
4+
type ModuleState = {};
5+
6+
const make_pretty_bytes = (
7+
_: ModuleState,
8+
value: number,
9+
callbackId: number
10+
) => {
11+
const byte_str = pretty_bytes(value);
12+
const result = new TextEncoder().encode(byte_str);
13+
Dart.send_value(callbackId, result);
14+
};
15+
16+
const make_pretty_ms = (_: ModuleState, value: number, callbackId: number) => {
17+
const byte_str = pretty_ms(value);
18+
const result = new TextEncoder().encode(byte_str);
19+
Dart.send_value(callbackId, result);
20+
};
21+
22+
export default {
23+
init: (..._: any[]): ModuleState => {
24+
return {};
25+
},
26+
functions: {
27+
make_pretty_bytes,
28+
make_pretty_ms,
29+
},
30+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// GENERATED FILE — DO NOT MODIFY BY HAND
2+
// This file was generated from package.json
3+
4+
const packageVersion = '1.0.0';
5+
6+
const packageSource = r'''
7+
var S=Object.defineProperty;var D=Object.getOwnPropertySymbols;var F=Object.prototype.hasOwnProperty,w=Object.prototype.propertyIsEnumerable;var B=(e,t,n)=>t in e?S(e,t,{enumerable:true,configurable:true,writable:true,value:n}):e[t]=n,m=(e,t)=>{for(var n in t||(t={}))F.call(t,n)&&B(e,n,t[n]);if(D)for(var n of D(t))w.call(t,n)&&B(e,n,t[n]);return e};var p=["B","kB","MB","GB","TB","PB","EB","ZB","YB"],Y=["B","KiB","MiB","GiB","TiB","PiB","EiB","ZiB","YiB"],k=["b","kbit","Mbit","Gbit","Tbit","Pbit","Ebit","Zbit","Ybit"],L=["b","kibit","Mibit","Gibit","Tibit","Pibit","Eibit","Zibit","Yibit"],x=(e,t,n)=>{let s=e;return typeof t=="string"||Array.isArray(t)?s=e.toLocaleString(t,n):(t===true||n!==void 0)&&(s=e.toLocaleString(void 0,n)),s},E=e=>{if(typeof e=="number")return Math.log10(e);let t=e.toString(10);return t.length+Math.log10("0."+t.slice(0,15))},P=e=>typeof e=="number"?Math.log(e):E(e)*Math.log(10),U=(e,t)=>{if(typeof e=="number")return e/t;let n=e/BigInt(t),s=e%BigInt(t);return Number(n)+Number(s)/t};function g(e,t){if(typeof e!="bigint"&&!Number.isFinite(e))throw new TypeError(`Expected a finite number, got ${typeof e}: ${e}`);t=m({bits:false,binary:false,space:true},t);let n=t.bits?t.binary?L:k:t.binary?Y:p,s=t.space?" ":"";if(t.signed&&(typeof e=="number"?e===0:e===BigInt(0)))return ` 0${s}${n[0]}`;let c=e<0,b=c?"-":t.signed?"+":"";c&&(e=-e);let i;if(t.minimumFractionDigits!==void 0&&(i={minimumFractionDigits:t.minimumFractionDigits}),t.maximumFractionDigits!==void 0&&(i=m({maximumFractionDigits:t.maximumFractionDigits},i)),e<1){let a=x(e,t.locale,i);return b+a+s+n[0]}let o=Math.min(Math.floor(t.binary?P(e)/Math.log(1024):E(e)/3),n.length-1);e=U(e,(t.binary?1024:1e3)**o),i||(e=e.toPrecision(3));let f=x(Number(e),t.locale,i),y=n[o];return b+f+s+y}var T=e=>Number.isFinite(e)?e:0;function Z(e){return {days:Math.trunc(e/864e5),hours:Math.trunc(e/36e5%24),minutes:Math.trunc(e/6e4%60),seconds:Math.trunc(e/1e3%60),milliseconds:Math.trunc(e%1e3),microseconds:Math.trunc(T(e*1e3)%1e3),nanoseconds:Math.trunc(T(e*1e6)%1e3)}}function $(e){return {days:e/BigInt(86400000),hours:e/BigInt(3600000)%BigInt(24),minutes:e/BigInt(60000)%BigInt(60),seconds:e/BigInt(1000)%BigInt(60),milliseconds:e%BigInt(1000),microseconds:BigInt(0),nanoseconds:BigInt(0)}}function M(e){switch(typeof e){case "number":{if(Number.isFinite(e))return Z(e);break}case "bigint":return $(e)}throw new TypeError("Expected a finite number or bigint")}var A=e=>e===0||e===BigInt(0),C=(e,t)=>t===1||t===BigInt(1)?e:`${e}s`,G=1e-7,V=BigInt(24)*BigInt(60)*BigInt(60)*BigInt(1000);function N(e,t){let n=typeof e=="bigint";if(!n&&!Number.isFinite(e))throw new TypeError("Expected a finite number or bigint");t=m({},t);let s=e<0?"-":"";e=e<0?-e:e,t.colonNotation&&(t.compact=false,t.formatSubMilliseconds=false,t.separateMilliseconds=false,t.verbose=false),t.compact&&(t.unitCount=1,t.secondsDecimalDigits=0,t.millisecondsDecimalDigits=0);let c=[],b=(a,u)=>{let d=Math.floor(a*10**u+G);return (Math.round(d)/10**u).toFixed(u)},i=(a,u,d,r)=>{if(!((c.length===0||!t.colonNotation)&&A(a)&&!(t.colonNotation&&d==="m"))){if(r!=null||(r=String(a)),t.colonNotation){let l=r.includes(".")?r.split(".")[0].length:r.length,h=c.length>0?2:1;r="0".repeat(Math.max(0,h-l))+r;}else r+=t.verbose?" "+C(u,a):d;c.push(r);}},o=M(e),f=BigInt(o.days);if(t.hideYearAndDays?i(BigInt(f)*BigInt(24)+BigInt(o.hours),"hour","h"):(t.hideYear?i(f,"day","d"):(i(f/BigInt(365),"year","y"),i(f%BigInt(365),"day","d")),i(Number(o.hours),"hour","h")),i(Number(o.minutes),"minute","m"),!t.hideSeconds)if(t.separateMilliseconds||t.formatSubMilliseconds||!t.colonNotation&&e<1e3){let a=Number(o.seconds),u=Number(o.milliseconds),d=Number(o.microseconds),r=Number(o.nanoseconds);if(i(a,"second","s"),t.formatSubMilliseconds)i(u,"millisecond","ms"),i(d,"microsecond","\xB5s"),i(r,"nanosecond","ns");else {let l=u+d/1e3+r/1e6,h=typeof t.millisecondsDecimalDigits=="number"?t.millisecondsDecimalDigits:0,I=l>=1?Math.round(l):Math.ceil(l),_=h?l.toFixed(h):I;i(Number.parseFloat(_),"millisecond","ms",_);}}else {let a=(n?Number(e%V):e)/1e3%60,u=typeof t.secondsDecimalDigits=="number"?t.secondsDecimalDigits:1,d=b(a,u),r=t.keepDecimalsOnWholeSeconds?d:d.replace(/\.0+$/,"");i(Number.parseFloat(r),"second","s",r);}if(c.length===0)return s+"0"+(t.verbose?" milliseconds":"ms");let y=t.colonNotation?":":" ";return typeof t.unitCount=="number"&&(c=c.slice(0,Math.max(t.unitCount,1))),s+c.join(y)}var j=(e,t,n)=>{let s=g(t),c=new TextEncoder().encode(s);Dart.send_value(n,c);},z=(e,t,n)=>{let s=N(t),c=new TextEncoder().encode(s);Dart.send_value(n,c);},v={init:(...e)=>({}),functions:{make_pretty_bytes:j,make_pretty_ms:z}};export{v as default};
8+
''';

0 commit comments

Comments
 (0)