Running on JavaScript#
Xarpite supports running on JavaScript.
Handling JavaScript Objects#
JavaScript objects are represented by the JS_OBJECT class.
Implicit Type Conversion#
In some situations such as function calls, automatic conversion is performed between Xarpite and JavaScript.
| Source Xarpite |
Target JavaScript |
|---|---|
JS_OBJECT |
As-is value |
INT |
Number |
DOUBLE |
Number |
STRING |
String |
BOOLEAN |
Boolean |
ARRAY |
Array |
NULL |
null |
FUNCTION |
Function |
| Others | Error for unsupported |
| Source JavaScript |
Target Xarpite |
|---|---|
| As-is value | JS_OBJECT |
Integer Number |
INT |
Decimal Number |
DOUBLE |
String |
STRING |
Boolean |
BOOLEAN |
Array |
ARRAY |
null |
NULL |
undefined |
NULL |
| Others | JS_OBJECT |
Various Features#
Function Calls#
JavaScript functions can be called like Xarpite functions.
Implicit type conversion is performed for arguments and return values.
Property Access#
js_object.property retrieves a JavaScript property.
js_object.property = value also allows assignment.
Both perform implicit type conversion.
obj := JS('({a: 100})')
obj.b = obj.a + 23
obj.b
# 123
Method Calls#
js_object::method(arguments) calls a method of a JavaScript object.
Arguments and return values are implicitly type-converted.
date := JS("new Date(946652400000)")
date::getFullYear()
# 2000
JavaScript Version Built-in Constants and Functions#
Constants and functions available only in the JavaScript version of Xarpite.
JS_OBJECT JavaScript Object Class#
JS_OBJECT is a class representing general JavaScript objects.
::new(arguments) Calling JavaScript Constructors#
jsObject::new(argument: VALUE; ...): JS_OBJECT
Calls a JavaScript function object as a constructor.
Each argument and return value undergoes implicit type conversion.
Basket := JS(%>
function Basket(item) {
this.item = item
}
Basket.prototype.toString = function() {
return "Basket[" + this.item + "]";
}
Basket;
<%)
Basket::new("apple")
# Basket[apple]
OUT Output to Console#
OUT(value: VALUE): NULL
Outputs the value to the output field determined for each web application.
WINDOW Get window Object#
WINDOW: JS_OBJECT | NULL
Returns the window object if accessible from that execution environment.
PWD Get Current Page URL#
PWD: STRING
The URL of the currently displayed page.
PWD
# https://example.com/page
This constant returns the URL of the current page rather than the parent directory (unlike the CLI version), based on the interpretation that it provides the “reference origin” path rather than the “parent hierarchy.”
This difference is due to the different handling of paths in file systems and URLs.
JS Execute JavaScript Code#
JS(code: STRING): VALUE
Executes the JavaScript code of the first argument.
The return value undergoes implicit type conversion.
JS('1 + 2')
# 3
JS('(function(a, b) {
return a + b;
})(1, 2)')
# 3
ASYNC Generate Asynchronous Function#
ASYNC(function: FUNCTION): JS_OBJECT
Converts the Xarpite function of the first argument to a JavaScript async function.
Normally, Xarpite functions are converted to non-async JavaScript functions through implicit type conversion.
This function allows you to explicitly generate async functions.
AWAIT(JS("Promise")::new(ASYNC(callback -> (
SLEEP(100)
callback(123)
))))
# 123
AWAIT Get Promise Result#
AWAIT(promise: JS_OBJECT): VALUE
Retrieves the result of the Promise of the first argument by suspending.
The result undergoes implicit type conversion.
Promises can be explicitly generated with a constructor or generated by calling async functions.
promise := JS('new Promise(callback => callback("apple"))')
AWAIT(promise)
# apple
async_function := JS('async () => "apple"')
promise := async_function()
AWAIT(promise)
# apple