Add include_standard_packages to Definitions.

This commit is contained in:
Stephen Chung 2022-07-29 13:39:51 +08:00
parent 52edded841
commit 13a1147dea
8 changed files with 621 additions and 96 deletions

View File

@ -677,6 +677,8 @@ op **(u64, int) -> u64;
op **(u8, int) -> u8;
op +(Decimal) -> Decimal;
op +(int) -> int;
op +(f32) -> f32;
@ -793,6 +795,8 @@ op +=(Instant, float) -> ();
/// Add the specified number of `seconds` to the timestamp.
op +=(Instant, int) -> ();
op -(Decimal) -> Decimal;
op -(int) -> int;
op -(f32) -> f32;
@ -1121,6 +1125,9 @@ op ^(u64, u64) -> u64;
op ^(u8, u8) -> u8;
/// Return the absolute value of the decimal number.
fn abs(x: Decimal) -> Decimal;
/// Return the absolute value of the number.
fn abs(x: int) -> int;
@ -1293,6 +1300,13 @@ fn atan(x: float, y: float) -> float;
/// Return the arc-hyperbolic-tangent of the floating-point number, in radians.
fn atanh(x: float) -> float;
/// Get an array of object maps containing the function calls stack.
///
/// If there is no debugging interface registered, an empty array is returned.
///
/// An array of strings is returned under `no_object`.
fn back_trace() -> Array;
/// Return an iterator over all the bits in the number.
///
/// # Example
@ -1406,6 +1420,9 @@ fn blob(len: int, value: int) -> Blob;
/// ```
fn bytes(string: String) -> int;
/// Return the smallest whole number larger than or equals to the decimal number.
fn ceiling(x: Decimal) -> Decimal;
/// Return the smallest whole number larger than or equals to the floating-point number.
fn ceiling(x: float) -> float;
@ -1547,6 +1564,9 @@ fn clear(string: String) -> ();
/// ```
fn contains(array: Array, value: ?) -> bool;
/// Return the cosine of the decimal number in radians.
fn cos(x: Decimal) -> Decimal;
/// Return the cosine of the floating-point number in radians.
fn cos(x: float) -> float;
@ -1960,6 +1980,9 @@ fn end(range: InclusiveRange) -> int;
/// ```
fn ends_with(string: String, match_string: String) -> bool;
/// Return the exponential of the decimal number.
fn exp(x: Decimal) -> Decimal;
/// Return the exponential of the floating-point number.
fn exp(x: float) -> float;
@ -2170,9 +2193,15 @@ fn filter(array: Array, filter: FnPtr) -> Array;
/// ```
fn filter(array: Array, filter_func: String) -> Array;
/// Return the largest whole number less than or equals to the decimal number.
fn floor(x: Decimal) -> Decimal;
/// Return the largest whole number less than or equals to the floating-point number.
fn floor(x: float) -> float;
/// Return the fractional part of the decimal number.
fn fraction(x: Decimal) -> Decimal;
/// Return the fractional part of the floating-point number.
fn fraction(x: float) -> float;
@ -2274,6 +2303,9 @@ fn get bits(value: int) -> Iterator<bool>;
/// ```
fn get bytes(string: String) -> int;
/// Return the smallest whole number larger than or equals to the decimal number.
fn get ceiling(x: Decimal) -> Decimal;
/// Return the smallest whole number larger than or equals to the floating-point number.
fn get ceiling(x: float) -> float;
@ -2307,12 +2339,21 @@ fn get end(range: ExclusiveRange) -> int;
/// Return the end of the inclusive range.
fn get end(range: InclusiveRange) -> int;
/// Return the largest whole number less than or equals to the decimal number.
fn get floor(x: Decimal) -> Decimal;
/// Return the largest whole number less than or equals to the floating-point number.
fn get floor(x: float) -> float;
/// Return the fractional part of the decimal number.
fn get fraction(x: Decimal) -> Decimal;
/// Return the fractional part of the floating-point number.
fn get fraction(x: float) -> float;
/// Return the integral part of the decimal number.
fn get int(x: Decimal) -> Decimal;
/// Return the integral part of the floating-point number.
fn get int(x: float) -> float;
@ -2408,6 +2449,9 @@ fn get is_odd(x: u64) -> bool;
/// Return true if the number is odd.
fn get is_odd(x: u8) -> bool;
/// Return true if the decimal number is zero.
fn get is_zero(x: Decimal) -> bool;
/// Return true if the number is zero.
fn get is_zero(x: int) -> bool;
@ -2484,6 +2528,10 @@ fn get len(string: String) -> int;
/// ```
fn get name(fn_ptr: FnPtr) -> String;
/// Return the nearest whole number closest to the decimal number.
/// Always round mid-point towards the closest even number.
fn get round(x: Decimal) -> Decimal;
/// Return the nearest whole number closest to the floating-point number.
/// Rounds away from zero.
fn get round(x: float) -> float;
@ -2848,6 +2896,9 @@ fn insert(array: Array, index: int, item: ?) -> ();
/// ```
fn insert(blob: Blob, index: int, value: int) -> ();
/// Return the integral part of the decimal number.
fn int(x: Decimal) -> Decimal;
/// Return the integral part of the floating-point number.
fn int(x: float) -> float;
@ -2943,6 +2994,9 @@ fn is_odd(x: u64) -> bool;
/// Return true if the number is odd.
fn is_odd(x: u8) -> bool;
/// Return true if the decimal number is zero.
fn is_zero(x: Decimal) -> bool;
/// Return true if the number is zero.
fn is_zero(x: int) -> bool;
@ -3020,9 +3074,15 @@ fn len(map: Map) -> int;
/// ```
fn len(string: String) -> int;
/// Return the natural log of the decimal number.
fn ln(x: Decimal) -> Decimal;
/// Return the natural log of the floating-point number.
fn ln(x: float) -> float;
/// Return the log of the decimal number with base 10.
fn log(x: Decimal) -> Decimal;
/// Return the log of the floating-point number with base 10.
fn log(x: float) -> float;
@ -3323,6 +3383,17 @@ fn parse_be_int(blob: Blob, range: RangeInclusive<int>) -> int;
/// ```
fn parse_be_int(blob: Blob, start: int, len: int) -> int;
/// Parse a string into a decimal number.
///
/// # Example
///
/// ```rhai
/// let x = parse_decimal("123.456");
///
/// print(x); // prints 123.456
/// ```
fn parse_decimal(string: String) -> Decimal;
/// Parse a string into a floating-point number.
///
/// # Example
@ -3700,6 +3771,27 @@ fn range(from: u64, to: u64) -> Iterator<u64>;
/// ```
fn range(from: u8, to: u8) -> Iterator<u8>;
/// Return an iterator over an exclusive range, each iteration increasing by `step`.
///
/// If `range` is reversed and `step` < 0, iteration goes backwards.
///
/// Otherwise, if `range` is empty, an empty iterator is returned.
///
/// # Example
///
/// ```rhai
/// // prints all values from 8 to 17 in steps of 3
/// for n in range(8..18, 3) {
/// print(n);
/// }
///
/// // prints all values down from 18 to 9 in steps of -3
/// for n in range(18..8, -3) {
/// print(n);
/// }
/// ```
fn range(range: Range<Decimal>, step: Decimal) -> Iterator<Decimal>;
/// Return an iterator over an exclusive range, each iteration increasing by `step`.
///
/// If `range` is reversed and `step` < 0, iteration goes backwards.
@ -3931,6 +4023,28 @@ fn range(range: Range<u64>, step: u64) -> Iterator<u64>;
/// ```
fn range(range: Range<u8>, step: u8) -> Iterator<u8>;
/// Return an iterator over the exclusive range of `from..to`, each iteration increasing by `step`.
/// The value `to` is never included.
///
/// If `from` > `to` and `step` < 0, iteration goes backwards.
///
/// If `from` > `to` and `step` > 0 or `from` < `to` and `step` < 0, an empty iterator is returned.
///
/// # Example
///
/// ```rhai
/// // prints all values from 8 to 17 in steps of 3
/// for n in range(8, 18, 3) {
/// print(n);
/// }
///
/// // prints all values down from 18 to 9 in steps of -3
/// for n in range(18, 8, -3) {
/// print(n);
/// }
/// ```
fn range(from: Decimal, to: Decimal, step: Decimal) -> Iterator<Decimal>;
/// Return an iterator over the exclusive range of `from..to`, each iteration increasing by `step`.
/// The value `to` is never included.
///
@ -4764,10 +4878,34 @@ fn reverse(array: Array) -> ();
/// ```
fn reverse(blob: Blob) -> ();
/// Return the nearest whole number closest to the decimal number.
/// Always round mid-point towards the closest even number.
fn round(x: Decimal) -> Decimal;
/// Return the nearest whole number closest to the floating-point number.
/// Rounds away from zero.
fn round(x: float) -> float;
/// Round the decimal number to the specified number of `digits` after the decimal point and return it.
/// Always round mid-point towards the closest even number.
fn round(x: Decimal, digits: int) -> Decimal;
/// Round the decimal number to the specified number of `digits` after the decimal point and return it.
/// Always round towards zero.
fn round_down(x: Decimal, digits: int) -> Decimal;
/// Round the decimal number to the specified number of `digits` after the decimal point and return it.
/// Always round mid-points towards zero.
fn round_half_down(x: Decimal, digits: int) -> Decimal;
/// Round the decimal number to the specified number of `digits` after the decimal point and return it.
/// Always round mid-points away from zero.
fn round_half_up(x: Decimal, digits: int) -> Decimal;
/// Round the decimal number to the specified number of `digits` after the decimal point and return it.
/// Always round away from zero.
fn round_up(x: Decimal, digits: int) -> Decimal;
/// Set the element at the `index` position in the array to a new `value`.
///
/// * If `index` < 0, position counts from the end of the array (`-1` is the last element).
@ -4993,6 +5131,13 @@ fn shift(array: Array) -> ?;
/// ```
fn shift(blob: Blob) -> int;
/// Return the sign (as an integer) of the decimal number according to the following:
///
/// * `0` if the number is zero
/// * `1` if the number is positive
/// * `-1` if the number is negative
fn sign(x: Decimal) -> int;
/// Return the sign (as an integer) of the number according to the following:
///
/// * `0` if the number is zero
@ -5042,6 +5187,9 @@ fn sign(x: i32) -> int;
/// * `-1` if the number is negative
fn sign(x: i8) -> int;
/// Return the sine of the decimal number in radians.
fn sin(x: Decimal) -> Decimal;
/// Return the sine of the floating-point number in radians.
fn sin(x: float) -> float;
@ -5471,6 +5619,9 @@ fn split_rev(string: String, delimiter: String, segments: int) -> Array;
/// ```
fn split_rev(string: String, delimiter: char, segments: int) -> Array;
/// Return the square root of the decimal number.
fn sqrt(x: Decimal) -> Decimal;
/// Return the square root of the floating-point number.
fn sqrt(x: float) -> float;
@ -5565,6 +5716,9 @@ fn sub_string(string: String, start: int, len: int) -> String;
/// ```
fn tag(value: ?) -> int;
/// Return the tangent of the decimal number in radians.
fn tan(x: Decimal) -> Decimal;
/// Return the tangent of the floating-point number in radians.
fn tan(x: float) -> float;
@ -5671,9 +5825,34 @@ fn to_debug(unit: ()) -> String;
/// Convert the boolean value into a string in debug format.
fn to_debug(value: bool) -> String;
/// Convert the floating-point number to decimal.
fn to_decimal(x: f32) -> Decimal;
/// Convert the floating-point number to decimal.
fn to_decimal(x: float) -> Decimal;
fn to_decimal(x: i16) -> Decimal;
fn to_decimal(x: i32) -> Decimal;
fn to_decimal(x: int) -> Decimal;
fn to_decimal(x: i8) -> Decimal;
fn to_decimal(x: u16) -> Decimal;
fn to_decimal(x: u32) -> Decimal;
fn to_decimal(x: u64) -> Decimal;
fn to_decimal(x: u8) -> Decimal;
/// Convert radians to degrees.
fn to_degrees(x: float) -> float;
/// Convert the decimal number to floating-point.
fn to_float(x: Decimal) -> float;
/// Convert the 32-bit floating-point number to 64-bit.
fn to_float(x: f32) -> float;
@ -5725,6 +5904,9 @@ fn to_hex(value: u64) -> String;
/// Convert the `value` into a string in hex format.
fn to_hex(value: u8) -> String;
/// Convert the decimal number into an integer.
fn to_int(x: Decimal) -> int;
fn to_int(x: char) -> int;
/// Convert the floating-point number into an integer.
@ -6247,10 +6429,10 @@ op |(u32, u32) -> u32;
op |(u64, u64) -> u64;
op |(u8, u8) -> u8;
module general_kenobi {
module general_kenobi {
/// Returns a string where "hello there" is repeated `n` times.
fn hello_there(n: int) -> String;
}
let hello_there;
let hello_there;

View File

@ -0,0 +1,10 @@
module static;
op minus(int, int) -> int;
module general_kenobi {
/// Returns a string where "hello there" is repeated `n` times.
fn hello_there(n: int) -> String;
}
let hello_there;

View File

@ -159,6 +159,8 @@ op **(u64, int) -> u64;
op **(u8, int) -> u8;
op +(Decimal) -> Decimal;
op +(int) -> int;
op +(f32) -> f32;
@ -275,6 +277,8 @@ op +=(Instant, float) -> ();
/// Add the specified number of `seconds` to the timestamp.
op +=(Instant, int) -> ();
op -(Decimal) -> Decimal;
op -(int) -> int;
op -(f32) -> f32;
@ -603,6 +607,9 @@ op ^(u64, u64) -> u64;
op ^(u8, u8) -> u8;
/// Return the absolute value of the decimal number.
fn abs(x: Decimal) -> Decimal;
/// Return the absolute value of the number.
fn abs(x: int) -> int;
@ -775,6 +782,13 @@ fn atan(x: float, y: float) -> float;
/// Return the arc-hyperbolic-tangent of the floating-point number, in radians.
fn atanh(x: float) -> float;
/// Get an array of object maps containing the function calls stack.
///
/// If there is no debugging interface registered, an empty array is returned.
///
/// An array of strings is returned under `no_object`.
fn back_trace() -> Array;
/// Return an iterator over all the bits in the number.
///
/// # Example
@ -888,6 +902,9 @@ fn blob(len: int, value: int) -> Blob;
/// ```
fn bytes(string: String) -> int;
/// Return the smallest whole number larger than or equals to the decimal number.
fn ceiling(x: Decimal) -> Decimal;
/// Return the smallest whole number larger than or equals to the floating-point number.
fn ceiling(x: float) -> float;
@ -1029,6 +1046,9 @@ fn clear(string: String) -> ();
/// ```
fn contains(array: Array, value: ?) -> bool;
/// Return the cosine of the decimal number in radians.
fn cos(x: Decimal) -> Decimal;
/// Return the cosine of the floating-point number in radians.
fn cos(x: float) -> float;
@ -1442,6 +1462,9 @@ fn end(range: InclusiveRange) -> int;
/// ```
fn ends_with(string: String, match_string: String) -> bool;
/// Return the exponential of the decimal number.
fn exp(x: Decimal) -> Decimal;
/// Return the exponential of the floating-point number.
fn exp(x: float) -> float;
@ -1652,9 +1675,15 @@ fn filter(array: Array, filter: FnPtr) -> Array;
/// ```
fn filter(array: Array, filter_func: String) -> Array;
/// Return the largest whole number less than or equals to the decimal number.
fn floor(x: Decimal) -> Decimal;
/// Return the largest whole number less than or equals to the floating-point number.
fn floor(x: float) -> float;
/// Return the fractional part of the decimal number.
fn fraction(x: Decimal) -> Decimal;
/// Return the fractional part of the floating-point number.
fn fraction(x: float) -> float;
@ -1756,6 +1785,9 @@ fn get bits(value: int) -> Iterator<bool>;
/// ```
fn get bytes(string: String) -> int;
/// Return the smallest whole number larger than or equals to the decimal number.
fn get ceiling(x: Decimal) -> Decimal;
/// Return the smallest whole number larger than or equals to the floating-point number.
fn get ceiling(x: float) -> float;
@ -1789,12 +1821,21 @@ fn get end(range: ExclusiveRange) -> int;
/// Return the end of the inclusive range.
fn get end(range: InclusiveRange) -> int;
/// Return the largest whole number less than or equals to the decimal number.
fn get floor(x: Decimal) -> Decimal;
/// Return the largest whole number less than or equals to the floating-point number.
fn get floor(x: float) -> float;
/// Return the fractional part of the decimal number.
fn get fraction(x: Decimal) -> Decimal;
/// Return the fractional part of the floating-point number.
fn get fraction(x: float) -> float;
/// Return the integral part of the decimal number.
fn get int(x: Decimal) -> Decimal;
/// Return the integral part of the floating-point number.
fn get int(x: float) -> float;
@ -1890,6 +1931,9 @@ fn get is_odd(x: u64) -> bool;
/// Return true if the number is odd.
fn get is_odd(x: u8) -> bool;
/// Return true if the decimal number is zero.
fn get is_zero(x: Decimal) -> bool;
/// Return true if the number is zero.
fn get is_zero(x: int) -> bool;
@ -1966,6 +2010,10 @@ fn get len(string: String) -> int;
/// ```
fn get name(fn_ptr: FnPtr) -> String;
/// Return the nearest whole number closest to the decimal number.
/// Always round mid-point towards the closest even number.
fn get round(x: Decimal) -> Decimal;
/// Return the nearest whole number closest to the floating-point number.
/// Rounds away from zero.
fn get round(x: float) -> float;
@ -2330,6 +2378,9 @@ fn insert(array: Array, index: int, item: ?) -> ();
/// ```
fn insert(blob: Blob, index: int, value: int) -> ();
/// Return the integral part of the decimal number.
fn int(x: Decimal) -> Decimal;
/// Return the integral part of the floating-point number.
fn int(x: float) -> float;
@ -2425,6 +2476,9 @@ fn is_odd(x: u64) -> bool;
/// Return true if the number is odd.
fn is_odd(x: u8) -> bool;
/// Return true if the decimal number is zero.
fn is_zero(x: Decimal) -> bool;
/// Return true if the number is zero.
fn is_zero(x: int) -> bool;
@ -2502,9 +2556,15 @@ fn len(map: Map) -> int;
/// ```
fn len(string: String) -> int;
/// Return the natural log of the decimal number.
fn ln(x: Decimal) -> Decimal;
/// Return the natural log of the floating-point number.
fn ln(x: float) -> float;
/// Return the log of the decimal number with base 10.
fn log(x: Decimal) -> Decimal;
/// Return the log of the floating-point number with base 10.
fn log(x: float) -> float;
@ -2805,6 +2865,17 @@ fn parse_be_int(blob: Blob, range: RangeInclusive<int>) -> int;
/// ```
fn parse_be_int(blob: Blob, start: int, len: int) -> int;
/// Parse a string into a decimal number.
///
/// # Example
///
/// ```rhai
/// let x = parse_decimal("123.456");
///
/// print(x); // prints 123.456
/// ```
fn parse_decimal(string: String) -> Decimal;
/// Parse a string into a floating-point number.
///
/// # Example
@ -3182,6 +3253,27 @@ fn range(from: u64, to: u64) -> Iterator<u64>;
/// ```
fn range(from: u8, to: u8) -> Iterator<u8>;
/// Return an iterator over an exclusive range, each iteration increasing by `step`.
///
/// If `range` is reversed and `step` < 0, iteration goes backwards.
///
/// Otherwise, if `range` is empty, an empty iterator is returned.
///
/// # Example
///
/// ```rhai
/// // prints all values from 8 to 17 in steps of 3
/// for n in range(8..18, 3) {
/// print(n);
/// }
///
/// // prints all values down from 18 to 9 in steps of -3
/// for n in range(18..8, -3) {
/// print(n);
/// }
/// ```
fn range(range: Range<Decimal>, step: Decimal) -> Iterator<Decimal>;
/// Return an iterator over an exclusive range, each iteration increasing by `step`.
///
/// If `range` is reversed and `step` < 0, iteration goes backwards.
@ -3413,6 +3505,28 @@ fn range(range: Range<u64>, step: u64) -> Iterator<u64>;
/// ```
fn range(range: Range<u8>, step: u8) -> Iterator<u8>;
/// Return an iterator over the exclusive range of `from..to`, each iteration increasing by `step`.
/// The value `to` is never included.
///
/// If `from` > `to` and `step` < 0, iteration goes backwards.
///
/// If `from` > `to` and `step` > 0 or `from` < `to` and `step` < 0, an empty iterator is returned.
///
/// # Example
///
/// ```rhai
/// // prints all values from 8 to 17 in steps of 3
/// for n in range(8, 18, 3) {
/// print(n);
/// }
///
/// // prints all values down from 18 to 9 in steps of -3
/// for n in range(18, 8, -3) {
/// print(n);
/// }
/// ```
fn range(from: Decimal, to: Decimal, step: Decimal) -> Iterator<Decimal>;
/// Return an iterator over the exclusive range of `from..to`, each iteration increasing by `step`.
/// The value `to` is never included.
///
@ -4246,10 +4360,34 @@ fn reverse(array: Array) -> ();
/// ```
fn reverse(blob: Blob) -> ();
/// Return the nearest whole number closest to the decimal number.
/// Always round mid-point towards the closest even number.
fn round(x: Decimal) -> Decimal;
/// Return the nearest whole number closest to the floating-point number.
/// Rounds away from zero.
fn round(x: float) -> float;
/// Round the decimal number to the specified number of `digits` after the decimal point and return it.
/// Always round mid-point towards the closest even number.
fn round(x: Decimal, digits: int) -> Decimal;
/// Round the decimal number to the specified number of `digits` after the decimal point and return it.
/// Always round towards zero.
fn round_down(x: Decimal, digits: int) -> Decimal;
/// Round the decimal number to the specified number of `digits` after the decimal point and return it.
/// Always round mid-points towards zero.
fn round_half_down(x: Decimal, digits: int) -> Decimal;
/// Round the decimal number to the specified number of `digits` after the decimal point and return it.
/// Always round mid-points away from zero.
fn round_half_up(x: Decimal, digits: int) -> Decimal;
/// Round the decimal number to the specified number of `digits` after the decimal point and return it.
/// Always round away from zero.
fn round_up(x: Decimal, digits: int) -> Decimal;
/// Set the element at the `index` position in the array to a new `value`.
///
/// * If `index` < 0, position counts from the end of the array (`-1` is the last element).
@ -4475,6 +4613,13 @@ fn shift(array: Array) -> ?;
/// ```
fn shift(blob: Blob) -> int;
/// Return the sign (as an integer) of the decimal number according to the following:
///
/// * `0` if the number is zero
/// * `1` if the number is positive
/// * `-1` if the number is negative
fn sign(x: Decimal) -> int;
/// Return the sign (as an integer) of the number according to the following:
///
/// * `0` if the number is zero
@ -4524,6 +4669,9 @@ fn sign(x: i32) -> int;
/// * `-1` if the number is negative
fn sign(x: i8) -> int;
/// Return the sine of the decimal number in radians.
fn sin(x: Decimal) -> Decimal;
/// Return the sine of the floating-point number in radians.
fn sin(x: float) -> float;
@ -4953,6 +5101,9 @@ fn split_rev(string: String, delimiter: String, segments: int) -> Array;
/// ```
fn split_rev(string: String, delimiter: char, segments: int) -> Array;
/// Return the square root of the decimal number.
fn sqrt(x: Decimal) -> Decimal;
/// Return the square root of the floating-point number.
fn sqrt(x: float) -> float;
@ -5047,6 +5198,9 @@ fn sub_string(string: String, start: int, len: int) -> String;
/// ```
fn tag(value: ?) -> int;
/// Return the tangent of the decimal number in radians.
fn tan(x: Decimal) -> Decimal;
/// Return the tangent of the floating-point number in radians.
fn tan(x: float) -> float;
@ -5153,9 +5307,34 @@ fn to_debug(unit: ()) -> String;
/// Convert the boolean value into a string in debug format.
fn to_debug(value: bool) -> String;
/// Convert the floating-point number to decimal.
fn to_decimal(x: f32) -> Decimal;
/// Convert the floating-point number to decimal.
fn to_decimal(x: float) -> Decimal;
fn to_decimal(x: i16) -> Decimal;
fn to_decimal(x: i32) -> Decimal;
fn to_decimal(x: int) -> Decimal;
fn to_decimal(x: i8) -> Decimal;
fn to_decimal(x: u16) -> Decimal;
fn to_decimal(x: u32) -> Decimal;
fn to_decimal(x: u64) -> Decimal;
fn to_decimal(x: u8) -> Decimal;
/// Convert radians to degrees.
fn to_degrees(x: float) -> float;
/// Convert the decimal number to floating-point.
fn to_float(x: Decimal) -> float;
/// Convert the 32-bit floating-point number to 64-bit.
fn to_float(x: f32) -> float;
@ -5207,6 +5386,9 @@ fn to_hex(value: u64) -> String;
/// Convert the `value` into a string in hex format.
fn to_hex(value: u8) -> String;
/// Convert the decimal number into an integer.
fn to_int(x: Decimal) -> int;
fn to_int(x: char) -> int;
/// Convert the floating-point number into an integer.

View File

@ -0,0 +1,49 @@
{
"modules": {
"general_kenobi": {
"functions": [
{
"baseHash": 2647973605745432570,
"fullHash": 14052888716942083368,
"namespace": "internal",
"access": "public",
"name": "hello_there",
"type": "native",
"numParams": 1,
"params": [
{
"name": "n",
"type": "i64"
}
],
"returnType": "String",
"signature": "hello_there(n: i64) -> String",
"docComments": [
"/// Returns a string where \"hello there\" is repeated `n` times."
]
}
]
}
},
"functions": [
{
"baseHash": 6399059862174416123,
"fullHash": 15886705420394126404,
"namespace": "global",
"access": "public",
"name": "minus",
"type": "native",
"numParams": 2,
"params": [
{
"type": "i64"
},
{
"type": "i64"
}
],
"returnType": "i64",
"signature": "minus(_: i64, _: i64) -> i64"
}
]
}

View File

@ -44,5 +44,21 @@ fn main() -> Result<(), Box<EvalAltResult>> {
.write_to_file("examples/definitions/.rhai/all_in_one.d.rhai")
.unwrap();
// Skip standard packages if not needed (e.g. they are provided elsewhere).
engine
.definitions_with_scope(&scope)
.include_standard_packages(false)
.write_to_file("examples/definitions/.rhai/all_in_one_without_standard.d.rhai")
.unwrap();
// Write function definitions as JSON.
let json = engine
.definitions()
.include_standard_packages(false)
.json()
.unwrap();
std::fs::write("examples/definitions/.rhai/defs.json", json).unwrap();
Ok(())
}

View File

@ -1,5 +1,4 @@
//! Module that defines functions to output definition files for [`Engine`].
//! Exported under the `internals` and `metadata` feature only.
#![cfg(feature = "internals")]
#![cfg(feature = "metadata")]
@ -66,6 +65,27 @@ impl Engine {
}
}
/// Internal configuration for module generation.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
#[non_exhaustive]
#[must_use]
pub struct DefinitionsConfig {
/// Write `module ...` headers in definition files (default `false`).
pub write_headers: bool,
/// Include standard packages (default `true`).
pub include_standard_packages: bool,
}
impl Default for DefinitionsConfig {
#[inline(always)]
fn default() -> Self {
Self {
write_headers: false,
include_standard_packages: true,
}
}
}
/// _(metadata, internals)_ Definitions helper type to generate definition files based on the
/// contents of an [`Engine`].
/// Exported under the `internals` and `metadata` feature only.
@ -79,7 +99,7 @@ pub struct Definitions<'e> {
config: DefinitionsConfig,
}
impl<'e> Definitions<'e> {
impl Definitions<'_> {
/// Write `module ...` headers in separate definitions, default `false`.
///
/// Headers are always present in content that is expected to be written to a file
@ -89,9 +109,32 @@ impl<'e> Definitions<'e> {
self.config.write_headers = headers;
self
}
/// Include standard packages when writing definition files.
#[inline(always)]
pub fn include_standard_packages(mut self, include_standard_packages: bool) -> Self {
self.config.include_standard_packages = include_standard_packages;
self
}
/// Get the [`Engine`].
#[inline(always)]
pub fn engine(&self) -> &Engine {
self.engine
}
/// Get the [`Scope`].
#[inline(always)]
#[must_use]
pub fn scope(&self) -> Option<&Scope> {
self.scope
}
/// Get the configuration.
#[inline(always)]
#[must_use]
pub(crate) fn config(&self) -> &DefinitionsConfig {
&self.config
}
}
impl<'e> Definitions<'e> {
impl Definitions<'_> {
/// Output all definition files returned from [`iter_files`][Definitions::iter_files] to a
/// specified directory.
///
@ -132,10 +175,12 @@ impl<'e> Definitions<'e> {
let mut def_file = String::from("module static;\n\n");
def_file += &self.builtin_functions_operators_impl(&config);
def_file += "\n";
def_file += &self.builtin_functions_impl(&config);
def_file += "\n";
if config.include_standard_packages {
def_file += &self.builtin_functions_operators_impl(&config);
def_file += "\n";
def_file += &self.builtin_functions_impl(&config);
def_file += "\n";
}
def_file += &self.static_module_impl(&config);
def_file += "\n";
@ -144,14 +189,16 @@ impl<'e> Definitions<'e> {
for (module_name, module_def) in self.modules_impl(&config) {
write!(
&mut def_file,
"module {module_name} {{\n\n{module_def}\n}}\n"
"\nmodule {module_name} {{\n{module_def}\n}}\n"
)
.unwrap();
}
def_file += "\n";
}
def_file += &self.scope_impl(&config);
def_file += &self.scope_items_impl(&config);
def_file += "\n";
def_file
}
@ -166,25 +213,31 @@ impl<'e> Definitions<'e> {
..self.config
};
IntoIterator::into_iter([
if config.include_standard_packages {
vec![
(
"__builtin__.d.rhai".to_string(),
self.builtin_functions_impl(&config),
),
(
"__builtin-operators__.d.rhai".to_string(),
self.builtin_functions_operators_impl(&config),
),
]
} else {
vec![]
}
.into_iter()
.chain(std::iter::once((
"__static__.d.rhai".to_string(),
self.static_module_impl(&config),
)))
.chain(self.scope.iter().map(move |_| {
(
"__builtin__.d.rhai".to_string(),
self.builtin_functions_impl(&config),
),
(
"__builtin-operators__.d.rhai".to_string(),
self.builtin_functions_operators_impl(&config),
),
(
"__static__.d.rhai".to_string(),
self.static_module_impl(&config),
),
])
.chain(
self.scope
.iter()
.map(move |_| ("__scope__.d.rhai".to_string(), self.scope_impl(&config))),
)
"__scope__.d.rhai".to_string(),
self.scope_items_impl(&config),
)
}))
.chain(
#[cfg(not(feature = "no_module"))]
{
@ -252,14 +305,17 @@ impl<'e> Definitions<'e> {
String::new()
};
let mut first = true;
for m in &self.engine.global_modules {
if !first {
s += "\n\n";
}
first = false;
m.write_definition(&mut s, self).unwrap();
}
self.engine
.global_modules
.iter()
.filter(|m| self.config.include_standard_packages || !m.standard)
.enumerate()
.for_each(|(i, m)| {
if i > 0 {
s += "\n\n";
}
m.write_definition(&mut s, self).unwrap();
});
s
}
@ -267,13 +323,13 @@ impl<'e> Definitions<'e> {
/// Return definitions for all items inside the [`Scope`], if any.
#[inline(always)]
#[must_use]
pub fn scope(&self) -> String {
self.scope_impl(&self.config)
pub fn scope_items(&self) -> String {
self.scope_items_impl(&self.config)
}
/// Return definitions for all items inside the [`Scope`], if any.
#[must_use]
fn scope_impl(&self, config: &DefinitionsConfig) -> String {
fn scope_items_impl(&self, config: &DefinitionsConfig) -> String {
let mut s = if config.write_headers {
String::from("module static;\n\n")
} else {
@ -324,14 +380,6 @@ impl<'e> Definitions<'e> {
}
}
/// Internal configuration for module generation.
#[derive(Debug, Default, Clone, Copy)]
#[non_exhaustive]
struct DefinitionsConfig {
/// Whether to write `module ...` headers.
write_headers: bool,
}
impl Module {
/// Return definitions for all items inside the [`Module`].
fn definition(&self, def: &Definitions) -> String {

View File

@ -11,6 +11,12 @@ use std::{
ops::{Range, RangeInclusive},
};
#[cfg(not(feature = "no_float"))]
use crate::FLOAT;
#[cfg(feature = "decimal")]
use rust_decimal::Decimal;
#[cfg(not(feature = "unchecked"))]
#[inline(always)]
fn std_add<T>(x: T, y: T) -> Option<T>
@ -348,10 +354,10 @@ def_package! {
}
#[cfg(not(feature = "no_float"))]
reg_range!(lib | step(regular_add) "range" => crate::FLOAT);
reg_range!(lib | step(regular_add) "range" => FLOAT);
#[cfg(feature = "decimal")]
reg_range!(lib | step "range" => rust_decimal::Decimal);
reg_range!(lib | step "range" => Decimal);
// Register string iterator
lib.set_iterator::<CharsStream>();

View File

@ -1,5 +1,4 @@
//! Serialization of functions metadata.
#![cfg(feature = "metadata")]
use crate::module::{calc_native_fn_hash, FuncInfo};
@ -195,6 +194,74 @@ impl<'a> From<&'a crate::Module> for ModuleMetadata<'a> {
}
}
/// Generate a list of all functions in JSON format.
pub fn gen_metadata_to_json(
engine: &Engine,
ast: Option<&AST>,
include_standard_packages: bool,
) -> serde_json::Result<String> {
let _ast = ast;
let mut global = ModuleMetadata::new();
#[cfg(not(feature = "no_module"))]
for (name, m) in &engine.global_sub_modules {
global.modules.insert(name, m.as_ref().into());
}
engine
.global_modules
.iter()
.filter(|m| include_standard_packages || !m.standard)
.flat_map(|m| m.iter_fn())
.for_each(|f| {
#[allow(unused_mut)]
let mut meta: FnMetadata = f.into();
#[cfg(not(feature = "no_module"))]
{
meta.namespace = FnNamespace::Global;
}
global.functions.push(meta);
});
#[cfg(not(feature = "no_function"))]
if let Some(ast) = _ast {
for f in ast.shared_lib().iter_fn() {
#[allow(unused_mut)]
let mut meta: FnMetadata = f.into();
#[cfg(not(feature = "no_module"))]
{
meta.namespace = FnNamespace::Global;
}
global.functions.push(meta);
}
}
global.functions.sort();
#[cfg(feature = "metadata")]
if let Some(ast) = _ast {
global.doc = ast.doc().into();
}
serde_json::to_string_pretty(&global)
}
#[cfg(feature = "internals")]
impl crate::api::definitions::Definitions<'_> {
/// Generate a list of all functions in JSON format.
///
/// Functions from the following sources are included:
/// 1) Functions defined in an [`AST`][crate::AST]
/// 2) Functions registered into the global namespace
/// 3) Functions in static modules
/// 4) Functions in registered global packages
/// 5) Functions in standard packages (optional)
#[inline(always)]
pub fn json(&self) -> serde_json::Result<String> {
gen_metadata_to_json(self.engine(), None, self.config().include_standard_packages)
}
}
impl Engine {
/// _(metadata)_ Generate a list of all functions (including those defined in an
/// [`AST`][crate::AST]) in JSON format.
@ -206,52 +273,13 @@ impl Engine {
/// 3) Functions in static modules
/// 4) Functions in registered global packages
/// 5) Functions in standard packages (optional)
#[inline(always)]
pub fn gen_fn_metadata_with_ast_to_json(
&self,
ast: &AST,
include_packages: bool,
include_standard_packages: bool,
) -> serde_json::Result<String> {
let _ast = ast;
let mut global = ModuleMetadata::new();
#[cfg(not(feature = "no_module"))]
for (name, m) in &self.global_sub_modules {
global.modules.insert(name, m.as_ref().into());
}
self.global_modules
.iter()
.filter(|m| include_packages || !m.standard)
.flat_map(|m| m.iter_fn())
.for_each(|f| {
#[allow(unused_mut)]
let mut meta: FnMetadata = f.into();
#[cfg(not(feature = "no_module"))]
{
meta.namespace = FnNamespace::Global;
}
global.functions.push(meta);
});
#[cfg(not(feature = "no_function"))]
for f in _ast.shared_lib().iter_fn() {
#[allow(unused_mut)]
let mut meta: FnMetadata = f.into();
#[cfg(not(feature = "no_module"))]
{
meta.namespace = FnNamespace::Global;
}
global.functions.push(meta);
}
global.functions.sort();
#[cfg(feature = "metadata")]
{
global.doc = ast.doc().into();
}
serde_json::to_string_pretty(&global)
gen_metadata_to_json(self, Some(ast), include_standard_packages)
}
/// Generate a list of all functions in JSON format.
@ -260,9 +288,13 @@ impl Engine {
/// Functions from the following sources are included:
/// 1) Functions registered into the global namespace
/// 2) Functions in static modules
/// 3) Functions in global modules (optional)
/// 3) Functions in registered global packages
/// 4) Functions in standard packages (optional)
#[inline(always)]
pub fn gen_fn_metadata_to_json(&self, include_packages: bool) -> serde_json::Result<String> {
self.gen_fn_metadata_with_ast_to_json(&AST::empty(), include_packages)
pub fn gen_fn_metadata_to_json(
&self,
include_standard_packages: bool,
) -> serde_json::Result<String> {
gen_metadata_to_json(self, None, include_standard_packages)
}
}