Version 4 (modified by jhorwitz, 13 years ago)

--

mod_parrot HLL Module Developer's Guide

Overview

This is the mod_parrot HLL module developer's guide. The target audience is developers wishing to embed their language in Apache using mod_parrot. The benefits of this are one-time compilation of scripts, a persistent execution environment, direct access to the Apache API, and the ability to write custom hooks in the embedded language. Some languages can be self-hosted, meaning the code to implement mod_foo is written in the "foo" language.

Most examples are taken from the PIR HLL module, with several from mod_perl6 to illustrate self-hosting.

Prerequisites

Any language targeted to Parrot can use mod_parrot to execute code in a persistent environment. However, to best take advantage of mod_parrot's features, including self-hosting, languages should support the following:

  • namespaces
  • lexical and global variables
  • an Parrot-compatible object model

Using mod_parrot without these features is still possible with some PIR scaffolding.

Bootstrapping

Each HLL in mod_parrot is contained in its own Apache module, known as an HLL module. All steps leading up to and including the registration of the HLL module with Apache is called bootstrapping. The bootstrapping process typically follows this procedure:

  1. Load the HLL compiler.
  2. Declare server and directory configuration hooks.
  3. Declare Apache directive hooks.
  4. Declare Apache directives.
  5. Declare metahandlers (hooks for Apache phases).
  6. Register the Apache module.

As of mod_parrot 0.5, step 1 must be written (or compiled to) PIR, but all subsequent steps can be written in the HLL itself (a self-hosting HLL module). The PIR bootstrap file MUST be compiled to bytecode and located in ModParrot/HLL/hllname.pbc in Parrot's library path. HLL code can be located anywhere, though conventions will eventually be defined. If there is HLL bootstrap code, it must be loaded and executed using PIR in the bootstrap file.

Bootstrap code must be placed in a PIR subroutine marked with the :load adverb so it is run when the file is loaded. This subroutine can be named or anonymous (using the :anon adverb).

Example: mod_perl6

The first part of the bootstrap file from mod_perl6 loads the compiler and supporting libraries, then executes Perl 6 code from mod_perl6.pm:

.sub __onload :anon :load
    load_bytecode 'languages/perl6/perl6.pbc'
    load_bytecode 'ModParrot/Apache/Module.pbc'
    load_bytecode 'ModParrot/Constants.pbc'

    # load mod_perl6.pm, which may be precompiled
    $P0 = compreg 'Perl6'
    $P1 = $P0.'compile'('use mod_perl6')
    $P1()

    ...

Configuration

Each HLL module provides two configuration data structures to Apache: server and directory. Server configurations are specific to the main server and individual virtual hosts. Directory configurations are specific to individual sections, which can be real directories or locations defined in the Apache configuration file. All configuration structures can be merged with parent configurations to implement inheritance or overriding behavior.

Creating HLL Configurations

Each Apache module is responsible for defining and creating its own configuration data structures. When Apache asks an HLL module for a server or directory configuration, mod_parrot will look for a subroutine in the ModParrot;HLL;hllname namespace to execute. Server configs are provided by server_create, while directory configs are provided by dir_create. These subroutines should create a data structure, possibly populated with default values, and return it. The type of the structure is up to the implementor, as long as it is a valid Parrot PMC.

Example: the PIR configuration subroutines

.namespace [ 'ModParrot'; 'HLL'; 'PIR' ]

.sub server_create
     $P0 = new 'Hash'
     .return($P0)
.end

.sub dir_create
     $P0 = new 'Hash'
     .return($P0)
.end

Merging HLL Configurations

Custom Apache Directives

Accessing mod_parrot's Configuration

Metahandlers

Naming Conventions

Registering Apache Hooks

The Context Object

Capturing Output

Registering the HLL Apache Module

Miscellany

Persistence

Sharing an Interpreter with Other Languages