Skip to content
Lucent
SEARCH LUCENT

Search guides, APIs, and examples.

GitHub

Very early and experimental. The language, the generated native code and every package API change without notice. Do not use Lucent in production.

1. Measure a trip in shared code

A module that measures distance and speed between GPS fixes, called from a screen.

Over eight steps, you'll build a trip tracker: a module that measures a trip from GPS fixes. Start from the app you set up in Install Lucent.

trip.lucent.ts
export type Fix = { latitude: number; longitude: number; time: number };
const EARTH_RADIUS_M = 6371000;
function radians(degrees: number): number {
return (degrees * Math.PI) / 180;
}
/** Meters between two fixes, along the Earth's surface. */
export function distance(a: Fix, b: Fix): number {
const dLat = radians(b.latitude - a.latitude);
const dLon = radians(b.longitude - a.longitude);
const h =
Math.sin(dLat / 2) ** 2 +
Math.cos(radians(a.latitude)) * Math.cos(radians(b.latitude)) * Math.sin(dLon / 2) ** 2;
return 2 * EARTH_RADIUS_M * Math.asin(Math.sqrt(h));
}
/** Meters per second from a to b. */
export function speed(a: Fix, b: Fix): number {
const seconds = (b.time - a.time) / 1000;
return seconds > 0 ? distance(a, b) / seconds : 0;
}
See the C++
m_trip.cpp
// Generated by Lucent from trip.lucent.ts. Do not edit.
#include "m_trip.h"
#pragma push_macro("EARTH_RADIUS_M")
#undef EARTH_RADIUS_M
#pragma push_macro("dLat")
#undef dLat
#pragma push_macro("dLon")
#undef dLon
#pragma push_macro("distance")
#undef distance
#pragma push_macro("h")
#undef h
#pragma push_macro("latitude")
#undef latitude
#pragma push_macro("longitude")
#undef longitude
#pragma push_macro("radians")
#undef radians
#pragma push_macro("seconds")
#undef seconds
#pragma push_macro("speed")
#undef speed
#pragma push_macro("time")
#undef time
namespace lucent_app {
double m_trip::radians(double p0_) {
#line 6 "trip.lucent.ts"
double v1_ = 3.141592653589793;
return p0_ * v1_ / 180.0;
}
double m_trip::distance(lucent::Ref<lucent_app::S_Fix> p0_, lucent::Ref<lucent_app::S_Fix> p1_) {
#line 11 "trip.lucent.ts"
double v2_ = p1_->latitude;
double v3_ = p0_->latitude;
double dLat = lucent_app::m_trip::radians(v2_ - v3_);
#line 12 "trip.lucent.ts"
double v6_ = p1_->longitude;
double v7_ = p0_->longitude;
double dLon = lucent_app::m_trip::radians(v6_ - v7_);
#line 14 "trip.lucent.ts"
double v12_ = dLat / 2.0;
double v13_ = lucent::math::sin(v12_);
double v15_ = lucent::jsPow(v13_, 2.0);
#line 15 "trip.lucent.ts"
double v16_ = p0_->latitude;
double v17_ = lucent_app::m_trip::radians(v16_);
double v18_ = lucent::math::cos(v17_);
double v19_ = p1_->latitude;
double v20_ = lucent_app::m_trip::radians(v19_);
double v21_ = lucent::math::cos(v20_);
double v22_ = v18_ * v21_;
double v25_ = dLon / 2.0;
double v26_ = lucent::math::sin(v25_);
#line 13 "trip.lucent.ts"
double h = v15_ + v22_ * lucent::jsPow(v26_, 2.0);
#line 16 "trip.lucent.ts"
double v33_ = 2.0 * 6371000.0;
double v35_ = lucent::math::sqrt(h);
double v36_ = lucent::math::asin(v35_);
return v33_ * v36_;
}
double m_trip::speed(lucent::Ref<lucent_app::S_Fix> p0_, lucent::Ref<lucent_app::S_Fix> p1_) {
#line 21 "trip.lucent.ts"
double v2_ = p1_->time;
double v3_ = p0_->time;
double seconds = (v2_ - v3_) / 1000.0;
#line 22 "trip.lucent.ts"
bool v9_ = seconds > 0.0;
double v14_{};
if (v9_) {
double v10_ = lucent_app::m_trip::distance(p0_, p1_);
v14_ = v10_ / seconds;
} else {
v14_ = 0.0;
}
#line 22 "trip.lucent.ts"
return v14_;
}
void m_trip::init() {
#line 3 "trip.lucent.ts"
lucent_app::m_trip::EARTH_RADIUS_M = 6371000.0;
}
} // namespace lucent_app
#pragma pop_macro("time")
#pragma pop_macro("speed")
#pragma pop_macro("seconds")
#pragma pop_macro("radians")
#pragma pop_macro("longitude")
#pragma pop_macro("latitude")
#pragma pop_macro("h")
#pragma pop_macro("distance")
#pragma pop_macro("dLon")
#pragma pop_macro("dLat")
#pragma pop_macro("EARTH_RADIUS_M")

Save the module as src/trip.lucent.ts. Fix is an object type. When JavaScript passes a fix, Lucent checks each field is a number, then copies the fix into a C++ struct.

radians and EARTH_RADIUS_M aren't exported, so JavaScript never sees them. Only distance and speed cross the boundary.

Build and run the app as in Your first module. The screen shows 3.16 km at 1.3 m/s: the walk from the Eiffel Tower to the Louvre, measured in C++.