Exemple #1
0
static int
noit_lua_module_config(noit_module_t *mod,
                       mtev_hash_table *options) {
  struct module_conf *mc;
  struct module_tls_conf *mtlsc;
  LMC_DECL(L, mod, object);

  mc = noit_module_get_userdata(mod);
  if(options) {
    mtevAssert(mc->options == NULL);
    mc->options = calloc(1, sizeof(*mc->options));
    mtev_hash_init(mc->options);
    mtev_hash_merge_as_dict(mc->options, options);
  }
  else options = mc->options;
  mtlsc = __get_module_tls_conf(&mod->hdr);
  if(mtlsc->configured) return mtlsc->configured_return;

  SETUP_CALL(L, object, "config", return 0);

  noit_lua_setup_module(L, mod);
  mtev_lua_hash_to_table(L, options);
  lua_pcall(L, 2, 1, 0);

  /* If rv == 0, the caller will free options. We've
   * already freed options, that would be bad. fudge -> 1 */
  RETURN_INT(L, object, "config",
             { mtlsc->configured = 1; mtlsc->configured_return = rv; });
Exemple #2
0
static int
noit_lua_module_onload(mtev_image_t *img) {
  int rv;
  lua_State *L;
  lua_module_closure_t *lmc;
  struct module_conf *mc;

  mc = mtev_image_get_userdata(img);

  lmc = lmc_tls_get(img);
  L = lmc->lua_state;
  if(!L) return -1;
  lua_getglobal(L, "require");
  lua_pushstring(L, mc->object);
  rv = lua_pcall(L, 1, 1, 0);
  if(rv) {
    int i;
    mtevL(nlerr, "lua: %s.onload failed\n", mc->object);
    i = lua_gettop(L);
    if(i>0) {
      if(lua_isstring(L, i)) {
        const char *err;
        size_t len;
        err = lua_tolstring(L, i, &len);
        mtevL(nlerr, "lua: %s\n", err);
      }
    }
    lua_pop(L,i);
    return -1;
  }
  lua_pop(L, lua_gettop(L));

  mtev_lua_pushmodule(L, mc->object);
  if(lua_isnil(L, -1)) {
    lua_pop(L, 1);
    mtevL(nlerr, "lua: no such object %s\n", mc->object);
    return -1;
  }
  lua_getfield(L, -1, "onload");
  lua_remove(L, -2);
  if(!lua_isfunction(L, -1)) {
    lua_pop(L, 1);
    /* No onload */
    return 0;
  }
  noit_lua_setup_module(L, (noit_module_t *)img);
  lua_pcall(L, 1, 1, 0);
  if(lua_isnumber(L, -1)) {
    int rv;
    rv = lua_tointeger(L, -1);
    lua_pop(L, 1);
    return rv;
  }
  mtevL(nlerr, "%s.onload must return a integer not %s (%s)\n", mc->object, mtev_lua_type_name(lua_type(L,-1)), lua_tostring(L,-1));
  lua_pop(L,1);
  return -1;
}
Exemple #3
0
static int
noit_lua_module_onload(noit_image_t *i) {
  int rv;
  lua_State *L;
  lua_module_closure_t *lmc;

  noit_lua_init();

  lmc = noit_image_get_userdata(i);
  L = lmc->lua_state;
  lua_getglobal(L, "require");
  lua_pushstring(L, lmc->object);
  rv = lua_pcall(L, 1, 1, 0);
  if(rv) {
    int i;
    noitL(nlerr, "lua: %s.onload failed\n", lmc->object);
    i = lua_gettop(L);
    if(i>0) {
      if(lua_isstring(L, i)) {
        const char *err;
        size_t len;
        err = lua_tolstring(L, i, &len);
        noitL(nlerr, "lua: %s\n", err);
      }
    }
    lua_pop(L,i);
    return -1;
  }
  lua_pop(L, lua_gettop(L));

  noit_lua_pushmodule(L, lmc->object);
  if(lua_isnil(L, -1)) {
    lua_pop(L, 1);
    noitL(nlerr, "lua: no such object %s\n", lmc->object);
    return -1;
  }
  lua_getfield(L, -1, "onload");
  lua_remove(L, -2);
  if(!lua_isfunction(L, -1)) {
    lua_pop(L, 1);
    /* No onload */
    return 0;
  }
  noit_lua_setup_module(L, (noit_module_t *)i);
  lua_pcall(L, 1, 1, 0);
  if(lua_isnumber(L, -1)) {
    int rv;
    rv = lua_tointeger(L, -1);
    lua_pop(L, 1);
    return rv;
  }
  lua_pop(L,1);
  noitL(nlerr, "%s.onload must return a integer\n", lmc->object);
  return -1;
}
Exemple #4
0
static int 
noit_lua_module_config(noit_module_t *mod,
                       noit_hash_table *options) {
  LMC_DECL(L, mod);
  SETUP_CALL(L, "config", return 0);

  noit_lua_setup_module(L, mod);
  noit_lua_hash_to_table(L, options);
  noit_hash_destroy(options, free, free);
  free(options);
  lua_pcall(L, 2, 1, 0);

  /* If rv == 0, the caller will free options. We've
   * already freed options, that would be bad. fudge -> 1 */
  RETURN_INT(L, "config", { rv = (rv == 0) ? 1 : rv; });