Skip to content
This repository has been archived by the owner on Jan 16, 2021. It is now read-only.

extending

Maxwell Krohn edited this page Dec 9, 2011 · 12 revisions

Extending OKWS via Runtime Pub Libraries

Slide 1

static ptr<expr_list_t>
split_vec_to_list (const vec<str> &v)
{
    ptr<expr_list_t> ret = New refcounted<expr_list_t> ();
    for (size_t i = 0; i < v.size (); i++) {
      ptr<expr_t> e;
      if (v[i]) e = New refcounted<expr_str_t> (v[i]);
      else e = expr_null_t::alloc ();
      ret->push_back (e);
    }
    return ret;
}

//-----------------------------------------------------------------------

PUB3_COMPILED_FN(split, "rs");

ptr<const expr_t>
split_t::v_eval_2 (eval_t *p, const vec<arg_t> &args) const
{
    ptr<rxx> rx = args[0]._r;
    str s = args[1]._s;
    vec<str> v;
    split (&v, *rx, s);
    return split_vec_to_list (v);
}

//--------------------------------------------------------------------------

lib_t::lib_t () : library_t ()
{
   _functions.push_back (New refcounted<split_t> ());
}

Slide 2

{%
    def include_foo (x) {
        include (x);
        return true;
    }
    include_foo (* "myfile.html" *);
%}

Slide 3

//-----------------------------------------------------------------------
// from libpub/pub3ast.T:

tamed void 
ast_node_t::publish (eval_t *p, status_ev_t ev) const
{
    tvars {
      xpub_status_t status;
    }

    p->set_lineno (lineno ());

    if (!might_block ()) {
      status = v_publish_nonblock (p);
    } else {
      twait { v_publish (p, mkevent (status)); }
    }
    ev->trigger (status);
}

Slide 4

bool
zone_html_t::might_block_uncached () const
{
    bool bl = false;
    for (size_t i = 0; !bl && i < _children.size (); i++) {
      if (_children[i]->might_block ()) {
	bl = true;
      }
    }
    return bl;
}
//-----------------------------------------------------------------------

Slide 5

{%
   locals { v : [ lambda () { sleep (* 1 *); return 1; },
                  lambda () { sleep (* 0, 500 *); return 2; },
                  lambda () { sleep (* 2, 1 *); return 3; } ],
             out : [] }
    out = shotgun(* v *);
    print (out);
%}

Slide 6

//---------------------------------------------------------------------

PUB3_COMPILED_FN_BLOCKING(shotgun, "l");

tamed void
shotgun_t::v_pub_to_val_2 (eval_t *e, const checked_args_t &args, cxev_t ev) 
  const
{
  tvars {
    vec<ptr<const callable_t> > v;
    ptr<expr_list_t> l;
    bool ok (true);
    size_t i;
    ptr<expr_t> out;
    vec<ptr<const expr_t> > results;
    ptr<expr_list_t> list_out;
    ptr<expr_list_t> fnargs;
  }
  

  l = args[0]._l;

  for (i = 0; ok && i < l->size (); i++) {
    ptr<const callable_t> f = (*l)[i]->to_callable ();
    if (!f) {
      strbuf b ("expected a list of lambdas, but arg %zu is not of proper type",
		i+1);
      report_error (e, b);
      ok = false;
    } else {
      v.push_back (f);
    }
  }

  if (ok) {
    results.setsize (v.size ());

    // The function passed should be a lambda that doesn't take an arguments,
    fnargs = expr_list_t::alloc ();

    twait {
      for (i = 0; i < v.size (); i++) {
	v[i]->pub_to_val (e, fnargs, mkevent (results[i]));
      }
    }

    // If any of the callees fail, then give them a proper null, not a 
    // C++ null value.
    list_out = expr_list_t::alloc ();
    list_out->setsize (v.size ());
    for (i = 0; i < v.size (); i++) {
      if (results[i]) {
	(*list_out)[i] = results[i]->copy ();
      } else {
	(*list_out)[i] = expr_null_t::alloc ();
      }
    }
    out = list_out;
  }
  ev->trigger (out);
}

//-----------------------------------------------------------------------

Slide 7

Learn More

  • libpub/pub3expr.h --- all of the low lever expressions, like pub3::expr_list_t, pub3::expr_dict_t, and pub3::expr_int_t
  • libpub/pub3obj.h --- higher-level wrappers that wrap pub3::expr_ts and make convenient interfaces via [] and () operators.
  • libpub/pub3ast.h --- abstract syntax tree object definitions
  • libpub/parse.yy --- the grammar
Clone this wiki locally