Types in constants and variables for definitions.
This commit is contained in:
parent
2f7d6298e0
commit
040f28f5cd
@ -53,6 +53,7 @@ Enhancements
|
|||||||
* `is_empty` method is added to arrays, BLOB's, object maps, strings and ranges.
|
* `is_empty` method is added to arrays, BLOB's, object maps, strings and ranges.
|
||||||
* `StaticModuleResolver` now stores the path in the module's `id` field.
|
* `StaticModuleResolver` now stores the path in the module's `id` field.
|
||||||
* `Engine::module_resolver` is added to grant access to the `Engine`'s module resolver.
|
* `Engine::module_resolver` is added to grant access to the `Engine`'s module resolver.
|
||||||
|
* Constants and variables now have types in generated definition files.
|
||||||
|
|
||||||
|
|
||||||
Version 1.9.1
|
Version 1.9.1
|
||||||
|
@ -1211,7 +1211,7 @@ fn all(array: Array, filter: FnPtr) -> bool;
|
|||||||
/// let x = [1, 2, 3];
|
/// let x = [1, 2, 3];
|
||||||
/// let y = [true, 'x'];
|
/// let y = [true, 'x'];
|
||||||
///
|
///
|
||||||
/// x.push(y);
|
/// x.append(y);
|
||||||
///
|
///
|
||||||
/// print(x); // prints "[1, 2, 3, true, 'x']"
|
/// print(x); // prints "[1, 2, 3, true, 'x']"
|
||||||
/// ```
|
/// ```
|
||||||
@ -6480,8 +6480,12 @@ op |(u64, u64) -> u64;
|
|||||||
op |(u8, u8) -> u8;
|
op |(u8, u8) -> u8;
|
||||||
|
|
||||||
module general_kenobi {
|
module general_kenobi {
|
||||||
|
const CONSTANT: int;
|
||||||
|
|
||||||
/// Returns a string where "hello there" is repeated `n` times.
|
/// Returns a string where "hello there" is repeated `n` times.
|
||||||
fn hello_there(n: int) -> String;
|
fn hello_there(n: int) -> String;
|
||||||
}
|
}
|
||||||
|
|
||||||
let hello_there;
|
let hello_there: string;
|
||||||
|
|
||||||
|
const HELLO: string;
|
||||||
|
@ -3,8 +3,12 @@ module static;
|
|||||||
op minus(int, int) -> int;
|
op minus(int, int) -> int;
|
||||||
|
|
||||||
module general_kenobi {
|
module general_kenobi {
|
||||||
|
const CONSTANT: int;
|
||||||
|
|
||||||
/// Returns a string where "hello there" is repeated `n` times.
|
/// Returns a string where "hello there" is repeated `n` times.
|
||||||
fn hello_there(n: int) -> String;
|
fn hello_there(n: int) -> String;
|
||||||
}
|
}
|
||||||
|
|
||||||
let hello_there;
|
let hello_there: string;
|
||||||
|
|
||||||
|
const HELLO: string;
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
module static;
|
module static;
|
||||||
|
|
||||||
let hello_there;
|
let hello_there: string;
|
||||||
|
|
||||||
|
const HELLO: string;
|
@ -693,7 +693,7 @@ fn all(array: Array, filter: FnPtr) -> bool;
|
|||||||
/// let x = [1, 2, 3];
|
/// let x = [1, 2, 3];
|
||||||
/// let y = [true, 'x'];
|
/// let y = [true, 'x'];
|
||||||
///
|
///
|
||||||
/// x.push(y);
|
/// x.append(y);
|
||||||
///
|
///
|
||||||
/// print(x); // prints "[1, 2, 3, true, 'x']"
|
/// print(x); // prints "[1, 2, 3, true, 'x']"
|
||||||
/// ```
|
/// ```
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
module general_kenobi;
|
module general_kenobi;
|
||||||
|
|
||||||
|
const CONSTANT: int;
|
||||||
|
|
||||||
/// Returns a string where "hello there" is repeated `n` times.
|
/// Returns a string where "hello there" is repeated `n` times.
|
||||||
fn hello_there(n: int) -> String;
|
fn hello_there(n: int) -> String;
|
@ -3,8 +3,8 @@
|
|||||||
"general_kenobi": {
|
"general_kenobi": {
|
||||||
"functions": [
|
"functions": [
|
||||||
{
|
{
|
||||||
"baseHash": 14798413363692662073,
|
"baseHash": 727795846011184342,
|
||||||
"fullHash": 2039416761244929762,
|
"fullHash": 5101524478338862216,
|
||||||
"namespace": "internal",
|
"namespace": "internal",
|
||||||
"access": "public",
|
"access": "public",
|
||||||
"name": "hello_there",
|
"name": "hello_there",
|
||||||
@ -27,8 +27,8 @@
|
|||||||
},
|
},
|
||||||
"functions": [
|
"functions": [
|
||||||
{
|
{
|
||||||
"baseHash": 17487674894006547092,
|
"baseHash": 17133166385977770750,
|
||||||
"fullHash": 13058993152904417424,
|
"fullHash": 11299449021188202345,
|
||||||
"namespace": "global",
|
"namespace": "global",
|
||||||
"access": "public",
|
"access": "public",
|
||||||
"name": "minus",
|
"name": "minus",
|
||||||
|
@ -3,6 +3,9 @@ use rhai::{Engine, EvalAltResult, Scope};
|
|||||||
|
|
||||||
#[export_module]
|
#[export_module]
|
||||||
pub mod general_kenobi {
|
pub mod general_kenobi {
|
||||||
|
/// General Kenobi's Constant.
|
||||||
|
pub const CONSTANT: i64 = 42;
|
||||||
|
|
||||||
/// Returns a string where "hello there" is repeated `n` times.
|
/// Returns a string where "hello there" is repeated `n` times.
|
||||||
pub fn hello_there(n: i64) -> String {
|
pub fn hello_there(n: i64) -> String {
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
@ -17,6 +20,9 @@ fn main() -> Result<(), Box<EvalAltResult>> {
|
|||||||
// This variable will also show up in the definitions, since it will be part of the scope.
|
// This variable will also show up in the definitions, since it will be part of the scope.
|
||||||
scope.push("hello_there", "hello there");
|
scope.push("hello_there", "hello there");
|
||||||
|
|
||||||
|
// This constant will also show up in the definitions, since it will be part of the scope.
|
||||||
|
scope.push_constant("HELLO", "hello there");
|
||||||
|
|
||||||
#[cfg(not(feature = "no_module"))]
|
#[cfg(not(feature = "no_module"))]
|
||||||
engine.register_static_module("general_kenobi", exported_module!(general_kenobi).into());
|
engine.register_static_module("general_kenobi", exported_module!(general_kenobi).into());
|
||||||
|
|
||||||
|
@ -339,7 +339,7 @@ impl Definitions<'_> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if let Some(scope) = self.scope {
|
if let Some(scope) = self.scope {
|
||||||
scope.write_definition(&mut s).unwrap();
|
scope.write_definition(&mut s, self).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
s
|
s
|
||||||
@ -412,13 +412,15 @@ impl Module {
|
|||||||
let mut vars = self.iter_var().collect::<Vec<_>>();
|
let mut vars = self.iter_var().collect::<Vec<_>>();
|
||||||
vars.sort_by(|(a, _), (b, _)| a.cmp(b));
|
vars.sort_by(|(a, _), (b, _)| a.cmp(b));
|
||||||
|
|
||||||
for (name, _) in vars {
|
for (name, value) in vars {
|
||||||
if !first {
|
if !first {
|
||||||
writer.write_str("\n\n")?;
|
writer.write_str("\n\n")?;
|
||||||
}
|
}
|
||||||
first = false;
|
first = false;
|
||||||
|
|
||||||
write!(writer, "const {name}: ?;")?;
|
let ty = def_type_name(value.type_name(), def.engine);
|
||||||
|
|
||||||
|
write!(writer, "const {name}: {ty};")?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut func_infos = self.iter_fn().collect::<Vec<_>>();
|
let mut func_infos = self.iter_fn().collect::<Vec<_>>();
|
||||||
@ -554,17 +556,18 @@ fn def_type_name<'a>(ty: &'a str, engine: &'a Engine) -> Cow<'a, str> {
|
|||||||
|
|
||||||
impl Scope<'_> {
|
impl Scope<'_> {
|
||||||
/// _(metadata, internals)_ Return definitions for all items inside the [`Scope`].
|
/// _(metadata, internals)_ Return definitions for all items inside the [`Scope`].
|
||||||
fn write_definition(&self, writer: &mut dyn fmt::Write) -> fmt::Result {
|
fn write_definition(&self, writer: &mut dyn fmt::Write, def: &Definitions) -> fmt::Result {
|
||||||
let mut first = true;
|
let mut first = true;
|
||||||
for (name, constant, _) in self.iter_raw() {
|
for (name, constant, value) in self.iter_raw() {
|
||||||
if !first {
|
if !first {
|
||||||
writer.write_str("\n\n")?;
|
writer.write_str("\n\n")?;
|
||||||
}
|
}
|
||||||
first = false;
|
first = false;
|
||||||
|
|
||||||
let kw = if constant { Token::Const } else { Token::Let };
|
let kw = if constant { Token::Const } else { Token::Let };
|
||||||
|
let ty = def_type_name(value.type_name(), def.engine);
|
||||||
|
|
||||||
write!(writer, "{kw} {name};")?;
|
write!(writer, "{kw} {name}: {ty};")?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
Loading…
Reference in New Issue
Block a user