Skip to content

Primi 0.0.15

Compare
Choose a tag to compare
@smuuf smuuf released this 24 Mar 12:46
· 412 commits to master since this release
6ef3f52
  • New: Support binding native PHP functions as Primi variables and/or functions

    • More of a "backend" feature.
    • Those who want to use Primi as library now can predefine functions that are to be available during the execution. These can be passed in as ordinary PHP closures (PHP callables will be internally converted to PHP closures).
    • New class FunctionContainer now acts as a universal wrapper for both standard Primi functions and also for newly supported native PHP functions. Note: Functions created from PHP callables won't have access to the caller context, but passing arguments from user-land to predefined native functions is fully supported.
    • Arguments are passed as Primi value objects (eg. \Smuuf\Primi\Structures\StringValue). A Primi value object is then also expected as the return value.
  • New: Array value now has 4 new methods:

    • array.copy() - Create a deep copy of the array.

    • array.shuffle() - Shuffle the array (existing array is shuffled, no new array is created and/or returned).

    • array.random() - Returns a random item from the array.

    • array.map(fn) - Returns a new array where the fn function was applied to all items.

      image

  • Change: Invoked functions now capture the context of where it was defined. Previous behavior was that the context of the caller was passed into the function.

Example of binding native PHP functions:

use \Smuuf\Primi\Context;
use \Smuuf\Primi\Structures\FunctionContainer;
use \Smuuf\Primi\Structures\StringValue;
use \Smuuf\Primi\Structures\FuncValue;

$c = new Context;

// Build function container from a native PHP anonymous function...
$fnContainer = FunctionContainer::buildNative(function($a, $b) {
    return new StringValue($a->getInternalValue() . "_" . $b->getInternalValue())
});

// Store the function value in the context as variable.
$c->setVariable('fun_fun', new FuncValue($fnContainer)));

// At this point the function "fun_fun" (which expects two arguments and returns a string)
// is available in user-land...