Magic Cloud

An Open Source Application Generator


Magic Lambda Slots

This project provides the ability to create, modify, inspect, and delete dynamic slots for Magic. More specifically, this project provides the following slots.

/*
 * First we create a dynamic slot.
 */
slots.create:foo
   return-value:int:57

/*
 * Then we invoke it.
 */
signal:foo

After evaluation of the above Hyperlambda, the value of the [signal] node will be 57. Notice, if you invoke [slots.create] for a slot that has already been created, the old slot will be overwritten with the new lambda object you pass into it. Also notice that if you try to invoke a slot that doesn’t exist, or you try to get its content, an exception will be thrown.

Also realize that when you create a slot, it is not persisted in any ways - Implying if the web server process is restarted, your slot will disappear. To avoid this, put the creation of your slot inside a file in a folder called “magic.startup” in your module’s folder, e.g. “/files/modules/foo-module/magic.startup/create-slot.hl”. All files inside of modules that exists within a “magic.startup” folder will be executed every time your web server restarts for some reasons.

[slots.create]

Creates a dynamic slot that can be invoked using [signal]. Below is an example.

slots.create:foo
   return-value:int:57

Notice, slots aren’t serialised in any ways, implying if your web server restarts its process, your previously created dynamic slot is remove. If you want to make sure your slots becomes a permanent part of your application, you need to make sure you are somehow creating it during server startup, which can normally be done on a per module basis, by putting a Hyperlambda file into your module’s “magic.startup” folder, which makes sure the code is executed during startup of your web app.

[signal]

Invokes a previously created dynamic slot. Assuming you have executed the above code snippet, you can invoke the slot using the following Hyperlambda.

signal:foo

After evaluating the above Hyperlambda, the [signal] node will end up having a value of “57”.

Notice - This slot obeys by the rules of [whitelist] invocations from magic.lambda, allowing you to declare a list of dynamic slots, which are exclusively allowed to be invoked temporarily inside of an invocation to whitelist, allowing you to restrict some piece of potentially unsafe code to only signal a pre-defined list of dynamically created slots. Below is an example.

slots.create:foo1
   return-value:Safe
slots.create:foo2
   return-value:Unsafe
whitelist
   vocabulary
      signal
      signal:foo1
   .lambda

      // Assuming you have a [foo1] slot created, this will work
      signal:foo1

      // This will throw, since [foo2] is not whitelisted
      signal:foo2

Basically, only the slots declared in the above [vocabulary] will be allowed to be invoked inside of the above [.lambda] object. You must have your dynamic slots declared as signal:slot-name, such as the above illustrates. And in order to signal dynamic slots at all, you’ll need to (obviously) whitelist [signal] itself, as illustrated in the above code. Read more about [whitelist] in the magic.lambda project.

[slots.get]

Returns the entire lambda code for a previously created dynamic slot. Example can be found below.

slots.create:foo
   return-value:int:57
slots.get:foo

[slots.delete]

Deletes a previously created dynamic slot. The following example will throw an exception.

slots.create:foo
   return-value:int:57
slots.delete:foo

// Throws exception, since [foo] no longer exists.
slots.get:foo

[return-nodes]

Returns a bunch of nodes (lambda) from your slot. Can only be invoked from the inside of a dynamically created slot. Example below.

slots.create:foo
   return-nodes
      integer-value:int:57
      string-value:Foo bar
slots.invoke:foo

[return-value]

Returns a single value from your slot. Can only be invoked from the inside of a dynamically created slot. Example below.

slots.create:foo
   return-value:This becomes the returned value from your slot
slots.invoke:foo

[slots.exists]

Returns true if the specified slot exists. Example can be found below.

slots.create:foo
   return-value:Blah, blah, blah ...
slots.exists:foo
slots.exists:DOES-NOT-EXISTS

[slots.vocabulary]

Returns the names of all dynamically created slots to caller. Example can be found below.

slots.vocabulary

Project website

The source code for this repository can be found at github.com/polterguy/magic.lambda.slots, and you can provide feedback, provide bug reports, etc at the same place.

Quality gates

License

This project is the copyright(c) 2020-2021 of Thomas Hansen thomas@servergardens.com, and is licensed under the terms of the LGPL version 3, as published by the Free Software Foundation. See the enclosed LICENSE file for details.