Skip to content

Commit

Permalink
Feat: ftd.main-package variable and some refactor
Browse files Browse the repository at this point in the history
WARN: the `ftd.main-package` does not work yet. It's stored in the
`global` map in js output and it's not getting resolved when used in an
ftd file. This should be key on `ftd` object itself.

- `ftd.main-package`: A string variable that stores the name of the
  packaeg. This is useful to determine if a package is mounted in
  another package or if it's running standalone.
- Refactor: Create `HostBuiltins` type and use it instead of Array of
  fixed sized type everywhere. This makes it easy to add new builtins
  • Loading branch information
siddhantk232 committed Jan 31, 2025
1 parent 8d2368f commit 1ff5f6b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
8 changes: 4 additions & 4 deletions fastn-core/src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ fn cached_parse(
pub fn package_dependent_builtins(
pkg: &fastn_core::Package,
req_path: &str,
) -> [(String, fastn_resolved::Definition); 1] {
[(
"ftd#app-path".to_string(),
) -> ftd::interpreter::HostBuiltins {
[
fastn_core::host_builtins::app_path(pkg, req_path),
)]
fastn_core::host_builtins::main_package(pkg),
]
}

#[tracing::instrument(skip(lib))]
Expand Down
37 changes: 33 additions & 4 deletions fastn-core/src/host_builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
///
/// Visiting `/app/` in browser should render /app/test/
#[inline]
pub fn app_path(pkg: &fastn_core::Package, req_path: &str) -> fastn_resolved::Definition {
pub fn app_path(pkg: &fastn_core::Package, req_path: &str) -> (String, fastn_resolved::Definition) {
let prefix = pkg
.apps
.iter()
Expand All @@ -31,8 +31,9 @@ pub fn app_path(pkg: &fastn_core::Package, req_path: &str) -> fastn_resolved::De
.unwrap_or_default();
let prefix = prefix.trim_end_matches('/');

fastn_resolved::Definition::Function(fastn_resolved::Function {
name: "ftd#app-path".to_string(),
let name = "ftd#app-path".to_string();
let def = fastn_resolved::Definition::Function(fastn_resolved::Function {
name: name.clone(),
return_kind: fastn_resolved::KindData {
kind: fastn_resolved::Kind::string(),
caption: false,
Expand All @@ -57,5 +58,33 @@ pub fn app_path(pkg: &fastn_core::Package, req_path: &str) -> fastn_resolved::De
js: None,
line_number: 0,
external_implementation: false,
})
});

(name, def)
}

/// Ftd string variable that holds the name of the package.
///
/// Useful to determine if the package is run standalone or as a dependency:
#[inline]
pub fn main_package(pkg: &fastn_core::Package) -> (String, fastn_resolved::Definition) {
let name = "ftd#main-package".to_string();
let def = fastn_resolved::Definition::Variable(fastn_resolved::Variable {
name: name.clone(),
kind: fastn_resolved::Kind::string().into_kind_data(),
value: fastn_resolved::PropertyValue::Value {
value: fastn_resolved::Value::String {
text: pkg.name.clone(),
},
is_mutable: false,
line_number: 0,
},
conditional_value: vec![],
mutable: false,
is_static: false,
line_number: 0,
});


(name, def)
}
8 changes: 6 additions & 2 deletions ftd/src/interpreter/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ use ftd::interpreter::things::web_component::WebComponentDefinitionExt;
use ftd::interpreter::FunctionExt;
use ftd::interpreter::{ComponentExt, VariableExt};


/// Array with the size of how many builtins are provided by the host
pub type HostBuiltins = [(String, fastn_resolved::Definition); 2];

/// The `InterpreterState` struct is a representation of the state of an interpreter. It contains
/// information about the interpreter's current state and its progress through the code being
/// interpreted.
Expand Down Expand Up @@ -99,7 +103,7 @@ impl InterpreterState {
#[tracing::instrument(skip(builtins))]
fn new_with_expanded_builtins(
id: String,
builtins: Option<[(String, fastn_resolved::Definition); 1]>,
builtins: Option<HostBuiltins>,
) -> InterpreterState {
let mut bag = ftd::interpreter::default::builtins().clone();

Expand Down Expand Up @@ -978,7 +982,7 @@ pub fn interpret(id: &str, source: &str) -> ftd::interpreter::Result<Interpreter
pub fn interpret_with_line_number(
id: &str,
document: ParsedDocument,
builtin_overrides: Option<[(String, fastn_resolved::Definition); 1]>,
builtin_overrides: Option<HostBuiltins>,
) -> ftd::interpreter::Result<Interpreter> {
use itertools::Itertools;

Expand Down
2 changes: 2 additions & 0 deletions ftd/src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ pub use things::value::{PropertyValueExt, PropertyValueSourceExt, ValueExt};
pub use things::variable::VariableExt;
pub use things::ThingExt;

pub use main::HostBuiltins;

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("OtherError: {}", _0)]
Expand Down

0 comments on commit 1ff5f6b

Please sign in to comment.