Esempio n. 1
0
/* Socket connect method.
 *
 * Example:
 * s.connect('10.10.1.1', 7000) #Connects to 10.10.1.1:7000
 */
tp_obj kolibri_connect(TP)
{
    tp_obj self = TP_TYPE(TP_DICT);
    tp_obj  remote_addr_obj = TP_OBJ();
    __u32  remote_addr;
    __u32  remote_port = (__u32)TP_TYPE(TP_NUMBER).number.val;
    __u32  local_port  = tp_get(tp, self, tp_string("local_port")).number.val;
    __u32  socktype = (__u32)tp_get(tp, self, tp_string("type")).number.val;
    int  s = -1; /* Socket descriptor */


    if (remote_addr_obj.type == TP_NUMBER)
        remote_addr = remote_addr_obj.number.val;
    else if (remote_addr_obj.type == TP_STRING)
        inet_pton(tp, (const char *)remote_addr_obj.string.val, remote_addr_obj.string.len, &remote_addr);

    if (socktype == SOCK_STREAM)
        s = __menuet__open_TCP_socket(local_port, remote_port, remote_addr, 1);
    else if (socktype == SOCK_DGRAM)
        s = __menuet__open_UDP_socket(local_port, remote_port, remote_addr);
    if (s >= 0)
    {
        tp_set(tp, self, tp_string("socket"), tp_number(s));
        return tp_True;
    }
    else
        return tp_False;
}
Esempio n. 2
0
/* Socket listen method.
 * 
 * Example:
 * s.listen('10.10.1.1', 5000)
 */
tp_obj kolibri_listen(TP)
{
    tp_obj self = TP_TYPE(TP_DICT);
    tp_obj remote_addr_obj = TP_OBJ();
    __u32  remote_addr;
    __u32  remote_port = (__u32)TP_TYPE(TP_NUMBER).number.val;
    __u32  local_port  = tp_get(tp, self, tp_string("local_port")).number.val;
    __u32  socktype = (__u32)tp_get(tp, self, tp_string("type")).number.val;
    int  s = -1; /* Socket descriptor */

    if (socktype != SOCK_STREAM)
        tp_raise(tp_None, "IOError: attempt to listen on non-TCP socket", tp_None);

    if (remote_addr_obj.type == TP_NUMBER)
        remote_addr = remote_addr_obj.number.val;
    else if (remote_addr_obj.type == TP_STRING)
        inet_pton(tp, (const char *)remote_addr_obj.string.val, remote_addr_obj.string.len, &remote_addr);

    if ((s = __menuet__open_TCP_socket(local_port, remote_port, remote_addr, 0)) >= 0)
    {
        tp_set(tp, self, tp_string("socket"), tp_number(s));
        return tp_True;
    }
    else
        return tp_False;
}
Esempio n. 3
0
tp_obj initClasspyDFMenu(tp_vm *vm)
{
  tp_obj myClass = tp_class(vm);
  tp_set(vm,myClass, tp_string("__init__"), tp_fnc(vm,myCtorpyDFMenu));
  tp_set(vm,myClass, tp_string("__set__"), tp_fnc(vm,zzpy__pyDFMenu_set));
  tp_set(vm,myClass, tp_string("__get__"), tp_fnc(vm,zzpy__pyDFMenu_get));
  tp_set(vm,myClass, tp_string("help"), tp_fnc(vm,zzpy__pyDFMenu_help));
  return myClass;
}
Esempio n. 4
0
tp_obj kolibri_socket_module(TP)
{
    tp_obj socket_mod = tp_dict(tp);

    tp_set(tp, socket_mod, tp_string("AF_INET"), tp_number(AF_INET));
    tp_set(tp, socket_mod, tp_string("SOCK_STREAM"), tp_number(SOCK_STREAM));
    tp_set(tp, socket_mod, tp_string("SOCK_DGRAM"), tp_number(SOCK_DGRAM));
    tp_set(tp, socket_mod, tp_string("inet_pton"), tp_fnc(tp, kolibri_inet_pton));
    tp_set(tp, socket_mod, tp_string("socket"), tp_fnc(tp, kolibri_socket));
    return socket_mod;
}
Esempio n. 5
0
/**
   \fn  asObjectPointer
*/
void *TinyParams::asObjectPointer(void)
{
	preamble(TP_DICT);
	tp_obj cdata = tp_get(tp, obj, tp_string("cdata"));
	return cdata.data.val;

}
Esempio n. 6
0
tp_obj tp_str_index(TP) {
    tp_obj s = TP_OBJ();
    tp_obj v = TP_OBJ();
    int n = _tp_str_index(s,v);
    if (n >= 0) { return tp_number(n); }
    tp_raise(tp_None,tp_string("(tp_str_index) ValueError: substring not found"));
}
Esempio n. 7
0
tp_obj tp_ord(TP) {
    tp_obj s = TP_STR();
    if (s.string.len != 1) {
        tp_raise(tp_None,tp_string("(tp_ord) TypeError: ord() expected a character"));
    }
    return tp_number((unsigned char)s.string.val[0]);
}
Esempio n. 8
0
/* Function: tp_set
 * Attribute modification.
 * 
 * This is the counterpart of tp_get, it does the same as self[k] = v would do
 * in actual tinypy code.
 */
void tp_set(TP,tp_obj self, tp_obj k, tp_obj v) {
    int type = self.type;

    if (type == TP_DICT) {
        TP_META_BEGIN(self,"__set__");
            tp_call(tp,meta,tp_params_v(tp,2,k,v));
            return;
        TP_META_END;
        _tp_dict_set(tp,self.dict.val,k,v);
        return;
    } else if (type == TP_LIST) {
        if (k.type == TP_NUMBER) {
            _tp_list_set(tp,self.list.val,k.number.val,v,"tp_set");
            return;
        } else if (k.type == TP_NONE) {
            _tp_list_append(tp,self.list.val,v);
            return;
        } else if (k.type == TP_STRING) {
            if (tp_cmp(tp,tp_string("*"),k) == 0) {
                tp_params_v(tp,2,self,v); tp_extend(tp);
                return;
            }
        }
    }
    tp_raise(,tp_string("(tp_set) TypeError: object does not support item assignment"));
}
Esempio n. 9
0
/* Socket bind method.
 *
 * In KolibriOS it just sets local address and port.
 *
 * Example:
 * s.bind('10.10.1.2', 6000) #Connects to 10.10.1.2:6000
 */
tp_obj kolibri_bind(TP)
{
    tp_obj self = TP_TYPE(TP_DICT);
    tp_obj local_addr_obj = TP_OBJ();
    __u32  local_port = (__u32)TP_TYPE(TP_NUMBER).number.val;
    __u32  local_addr;

    if (local_addr_obj.type == TP_NUMBER)
        local_addr = local_addr_obj.number.val;
    else if (local_addr_obj.type == TP_STRING)
        inet_pton(tp, (const char *)local_addr_obj.string.val, local_addr_obj.string.len, &local_addr);

    tp_set(tp, self, tp_string("local_addr"), tp_number(local_addr));
    tp_set(tp, self, tp_string("local_port"), tp_number(local_port));
    return tp_None;
}
Esempio n. 10
0
int tp_cmp(TP,tp_obj a, tp_obj b) {
    if (a.type != b.type) { return a.type-b.type; }
    switch(a.type) {
        case TP_NONE: return 0;
        case TP_NUMBER: return _tp_sign(a.number.val-b.number.val);
        case TP_STRING: {
            int l = _tp_min(a.string.len,b.string.len);
            int v = memcmp(a.string.val,b.string.val,l);
            if (v == 0) {
                v = a.string.len-b.string.len;
            }
            return v;
        }
        case TP_LIST: {
            int n,v; for(n=0;n<_tp_min(a.list.val->len,b.list.val->len);n++) {
        tp_obj aa = a.list.val->items[n]; tp_obj bb = b.list.val->items[n];
            if (aa.type == TP_LIST && bb.type == TP_LIST) { v = aa.list.val-bb.list.val; } else { v = tp_cmp(tp,aa,bb); }
            if (v) { return v; } }
            return a.list.val->len-b.list.val->len;
        }
        case TP_DICT: return a.dict.val - b.dict.val;
        case TP_FNC: return a.fnc.info - b.fnc.info;
        case TP_DATA: return (char*)a.data.val - (char*)b.data.val;
    }
    tp_raise(0,tp_string("(tp_cmp) TypeError: ?"));
}
Esempio n. 11
0
/* Function: tp_iter
 * Iterate through a list or dict.
 *
 * If self is a list/string/dictionary, this will iterate over the
 * elements/characters/keys respectively, if k is an increasing index
 * starting with 0 up to the length of the object-1.
 *
 * In the case of a list of string, the returned items will correspond to the
 * item at index k. For a dictionary, no guarantees are made about the order.
 * You also cannot call the function with a specific k to get a specific
 * item -- it is only meant for iterating through all items, calling this
 * function len(self) times. Use <tp_get> to retrieve a specific item, and
 * <tp_len> to get the length.
 *
 * Parameters:
 * self - The object over which to iterate.
 * k - You must pass 0 on the first call, then increase it by 1 after each call,
 *     and don't call the function with k >= len(self).
 *
 * Returns:
 * The first (k = 0) or next (k = 1 .. len(self)-1) item in the iteration.
 */
tp_obj tp_iter(TP,tp_obj self, tp_obj k) {
    int type = self.type;
    if (type == TP_LIST || type == TP_STRING) { return tp_get(tp,self,k); }
    if (type == TP_DICT && k.type == TP_NUMBER) {
        return self.dict.val->items[_tp_dict_next(tp,self.dict.val)].key;
    }
    tp_raise(tp_None,tp_string("(tp_iter) TypeError: iteration over non-sequence"));
}
Esempio n. 12
0
static tp_obj zzpy__pyDFMenu_help(TP)
{
	PythonEngine *engine = (PythonEngine*)tp_get(tp, tp->builtins, tp_string("userdata")).data.val;

	engine->callEventHandlers(IScriptEngine::Information, NULL, -1, "addItem(str)\n");

	return tp_None;
};
Esempio n. 13
0
tp_obj tp_copy(TP) {
    tp_obj r = TP_OBJ();
    int type = r.type;
    if (type == TP_LIST) {
        return _tp_list_copy(tp,r);
    } else if (type == TP_DICT) {
        return _tp_dict_copy(tp,r);
    }
    tp_raise(tp_None,tp_string("(tp_copy) TypeError: ?"));
}
Esempio n. 14
0
tp_obj zzpy__pyDFMenu_get(tp_vm *vm)
{
  tp_obj self = tp_getraw(vm);
  IScriptEngine *engine = (IScriptEngine*)tp_get(vm, vm->builtins, tp_string("userdata")).data.val;
  IEditor *editor = engine->editor();
  TinyParams pm(vm);
  ADM_scriptDFMenuHelper *me=(ADM_scriptDFMenuHelper *)pm.asThis(&self, ADM_PYID_DF_INTEGER);
  char const *key = pm.asString();
  if (!strcmp(key, "index"))
  {
     if(!me) pm.raise("pyDFMenu:No this!");
     return tp_number(me->index());
  }
  if (!strcmp(key, "addItem"))
  {
     return tp_method(vm, self, zzpy_addItem);
  }
  return tp_get(vm, self, tp_string(key));
}
Esempio n. 15
0
tp_obj tp_save(TP) {
    char fname[256]; tp_cstr(tp,TP_STR(),fname,256);
    tp_obj v = TP_OBJ();
    FILE *f;
    f = fopen(fname,"wb");
    if (!f) { tp_raise(tp_None,tp_string("(tp_save) IOError: ?")); }
    fwrite(v.string.val,v.string.len,1,f);
    fclose(f);
    return tp_None;
}
Esempio n. 16
0
/* Function: tp_str
 * String representation of an object.
 *
 * Returns a string object representating self.
 */
tp_obj tp_str(TP,tp_obj self) {
    int type = self.type;
    if (type == TP_STRING) { return self; }
    if (type == TP_NUMBER) {
        tp_num v = self.number.val;
        if ((fabs(v)-fabs((long)v)) < 0.000001) { return tp_printf(tp,"%ld",(long)v); }
        return tp_printf(tp,"%f",v);
    } else if(type == TP_DICT) {
        return tp_printf(tp,"<dict 0x%x>",self.dict.val);
    } else if(type == TP_LIST) {
        return tp_printf(tp,"<list 0x%x>",self.list.val);
    } else if (type == TP_NONE) {
        return tp_string("None");
    } else if (type == TP_DATA) {
        return tp_printf(tp,"<data 0x%x>",self.data.val);
    } else if (type == TP_FNC) {
        return tp_printf(tp,"<fnc 0x%x>",self.fnc.info);
    }
    return tp_string("<?>");
}
Esempio n. 17
0
// Ctor (str)
static tp_obj myCtorpyDFMenu(tp_vm *vm)
{
  tp_obj self = tp_getraw(vm);
  TinyParams pm(vm);
  const char *p0 = pm.asString();
  ADM_scriptDFMenuHelper *me = new ADM_scriptDFMenuHelper(p0);
  tp_obj cdata = tp_data(vm, ADM_PYID_DF_INTEGER, me);
  cdata.data.info->xfree = myDtorpyDFMenu;
  tp_set(vm, self, tp_string("cdata"), cdata);
  return tp_None;
}
Esempio n. 18
0
tp_obj tp_str(tp_vm *tp, tp_obj self) {
    int type = obj_type(self);
    if (type == TP_STRING) { return self; }
    if (type == TP_NUMBER) {
        tp_num v = tp_number_val(self);
        if ((fabs(v)-fabs((long)v)) < 0.000001) { return tp_printf(tp,"%ld",(long)v); }
        return tp_printf(tp,"%f",v);
    } else if(type == TP_DICT) {
        return tp_printf(tp,"<dict 0x%x>", tp_dict_val(self));
    } else if(type == TP_LIST) {
        return tp_printf(tp,"<list 0x%x>",tp_list_val(self));
    } else if (type == TP_NONE) {
        return tp_string(tp, "None");
    } else if (type == TP_DATA) {
        return tp_printf(tp,"<data 0x%x>", tp_data_val(self));
    } else if (type == TP_FNC) {
        return tp_printf(tp,"<fnc 0x%x>", tp_fnc_val(self));
    }
    return tp_string(tp, "<?>");
}
Esempio n. 19
0
/* Function: tp_len
 * Returns the length of an object.
 *
 * Returns the number of items in a list or dict, or the length of a string.
 */
tp_obj tp_len(TP,tp_obj self) {
    int type = self.type;
    if (type == TP_STRING) {
        return tp_number(self.string.len);
    } else if (type == TP_DICT) {
        return tp_number(self.dict.val->len);
    } else if (type == TP_LIST) {
        return tp_number(self.list.val->len);
    }
    
    tp_raise(tp_None,tp_string("(tp_len) TypeError: len() of unsized object"));
}
Esempio n. 20
0
/* Function: tp_has
 * Checks if an object contains a key.
 *
 * Returns tp_True if self[k] exists, tp_False otherwise.
 */
tp_obj tp_has(TP,tp_obj self, tp_obj k) {
    int type = self.type;
    if (type == TP_DICT) {
        if (_tp_dict_find(tp,self.dict.val,k) != -1) { return tp_True; }
        return tp_False;
    } else if (type == TP_STRING && k.type == TP_STRING) {
        return tp_number(_tp_str_index(self,k)!=-1);
    } else if (type == TP_LIST) {
        return tp_number(_tp_list_find(tp,self.list.val,k)!=-1);
    }
    tp_raise(tp_None,tp_string("(tp_has) TypeError: iterable argument required"));
}
Esempio n. 21
0
void *TinyParams::asThis(tp_obj *self, int id)
{
	tp_obj cdata = tp_get(tp, *self, tp_string("cdata"));

	if (cdata.data.magic != id)
	{
		raise("Bad class : Expected %d, got %d\n", id, cdata.data.magic);
		\
	}

	return cdata.data.val;
}
Esempio n. 22
0
tp_vm *_tp_init(void) {
    int i;
    tp_vm *tp = (tp_vm*)calloc(sizeof(tp_vm),1);
    tp->time_limit = TP_NO_LIMIT;
    tp->clocks = clock();
    tp->time_elapsed = 0.0;
    tp->mem_limit = TP_NO_LIMIT;
    tp->mem_exceeded = 0;
    tp->mem_used = sizeof(tp_vm);
    tp->cur = 0;
    tp->jmp = 0;
    tp->ex = tp_None;
    tp->root = tp_list_nt(tp);
    for (i=0; i<256; i++) { tp->chars[i][0]=i; }
    tp_gc_init(tp);
    tp->_regs = tp_list(tp);
    for (i=0; i<TP_REGS; i++) { tp_set(tp,tp->_regs,tp_None,tp_None); }
    tp->builtins = tp_dict(tp);
    tp->modules = tp_dict(tp);
    tp->_params = tp_list(tp);
    for (i=0; i<TP_FRAMES; i++) { tp_set(tp,tp->_params,tp_None,tp_list(tp)); }
    tp_set(tp,tp->root,tp_None,tp->builtins);
    tp_set(tp,tp->root,tp_None,tp->modules);
    tp_set(tp,tp->root,tp_None,tp->_regs);
    tp_set(tp,tp->root,tp_None,tp->_params);
    tp_set(tp,tp->builtins,tp_string("MODULES"),tp->modules);
    tp_set(tp,tp->modules,tp_string("BUILTINS"),tp->builtins);
    tp_set(tp,tp->builtins,tp_string("BUILTINS"),tp->builtins);
    tp_obj sys = tp_dict(tp);
    tp_set(tp, sys, tp_string("version"), tp_string("tinypy 1.2+SVN"));
    tp_set(tp,tp->modules, tp_string("sys"), sys);
    tp->regs = tp->_regs.list.val->items;
    tp_full(tp);
    return tp;
}
Esempio n. 23
0
tp_obj tp_istype(TP) {
    tp_obj v = TP_OBJ();
    tp_obj t = TP_STR();
    if (tp_cmp(tp,t,tp_string("string")) == 0) { return tp_number(v.type == TP_STRING); }
    if (tp_cmp(tp,t,tp_string("list")) == 0) { return tp_number(v.type == TP_LIST); }
    if (tp_cmp(tp,t,tp_string("dict")) == 0) { return tp_number(v.type == TP_DICT); }
    if (tp_cmp(tp,t,tp_string("number")) == 0) { return tp_number(v.type == TP_NUMBER); }
    if (tp_cmp(tp,t,tp_string("fnc")) == 0) { return tp_number(v.type == TP_FNC && (v.fnc.ftype&2) == 0); }
    if (tp_cmp(tp,t,tp_string("method")) == 0) { return tp_number(v.type == TP_FNC && (v.fnc.ftype&2) != 0); }
    tp_raise(tp_None,tp_string("(is_type) TypeError: ?"));
}
Esempio n. 24
0
tp_obj tp_float(TP) {
    tp_obj v = TP_OBJ();
    int ord = TP_DEFAULT(tp_number(0)).number.val;
    int type = v.type;
    if (type == TP_NUMBER) { return v; }
    if (type == TP_STRING && v.string.len < 32) {
        char s[32]; memset(s,0,v.string.len+1);
        memcpy(s,v.string.val,v.string.len);
        if (strchr(s,'.')) { return tp_number(atof(s)); }
        return(tp_number(strtol(s,0,ord)));
    }
    tp_raise(tp_None,tp_string("(tp_float) TypeError: ?"));
}
Esempio n. 25
0
/* tp_frame_*/
void tp_frame(TP,tp_obj globals,tp_obj code,tp_obj *ret_dest) {
    tp_frame_ f;
    f.globals = globals;
    f.code = code;
    f.cur = (tp_code*)f.code.string.val;
    f.jmp = 0;
/*     fprintf(stderr,"tp->cur: %d\n",tp->cur);*/
    f.regs = (tp->cur <= 0?tp->regs:tp->frames[tp->cur].regs+tp->frames[tp->cur].cregs);
    
    f.regs[0] = f.globals;
    f.regs[1] = f.code;
    f.regs += TP_REGS_EXTRA;
    
    f.ret_dest = ret_dest;
    f.lineno = 0;
    f.line = tp_string("");
    f.name = tp_string("?");
    f.fname = tp_string("?");
    f.cregs = 0;
/*     return f;*/
    if (f.regs+(256+TP_REGS_EXTRA) >= tp->regs+TP_REGS || tp->cur >= TP_FRAMES-1) {
        tp_raise(,tp_string("(tp_frame) RuntimeError: stack overflow"));
    }
Esempio n. 26
0
/* Socket close method.
 *
 * Example: 
 * s.close() # s must be a socket object created by socket.
 *
 * Raises exception if socket was not opened. Otherwise returns True.
 */
static tp_obj kolibri_close_socket(TP)
{
    tp_obj self = TP_TYPE(TP_DICT);
    __u32  socktype;
    __u32  s;

    GET_SOCKET_DESCRIPTOR(self, s);

    socktype = (__u32)tp_get(tp, self, tp_string("type")).number.val;
    GET_SOCKET_DESCRIPTOR(self, s);
    if (socktype == SOCK_STREAM)
        __menuet__close_TCP_socket(s);
    else if (socktype == SOCK_DGRAM)
        __menuet__close_UDP_socket(s);
    return tp_True;
}
Esempio n. 27
0
tp_obj tp_strip(TP) {
    tp_obj o = TP_TYPE(TP_STRING);
    char const *v = o.string.val; int l = o.string.len;
    int i; int a = l, b = 0;
    tp_obj r;
    char *s;
    for (i=0; i<l; i++) {
        if (v[i] != ' ' && v[i] != '\n' && v[i] != '\t' && v[i] != '\r') {
            a = _tp_min(a,i); b = _tp_max(b,i+1);
        }
    }
    if ((b-a) < 0) { return tp_string(""); }
    r = tp_string_t(tp,b-a);
    s = r.string.info->s;
    memcpy(s,v+a,b-a);
    return tp_track(tp,r);
}
Esempio n. 28
0
int _tp_lookup_(TP,tp_obj self, tp_obj k, tp_obj *meta, int depth) {
    int n = _tp_dict_find(tp,self.dict.val,k);
    if (n != -1) {
        *meta = self.dict.val->items[n].val;
        return 1;
    }
    depth--; if (!depth) { tp_raise(0,tp_string("(tp_lookup) RuntimeError: maximum lookup depth exceeded")); }
    if (self.dict.dtype && self.dict.val->meta.type == TP_DICT && _tp_lookup_(tp,self.dict.val->meta,k,meta,depth)) {
        if (self.dict.dtype == 2 && meta->type == TP_FNC) {
            *meta = tp_fnc_new(tp,meta->fnc.ftype|2,
                meta->fnc.cfnc,meta->fnc.info->code,
                self,meta->fnc.info->globals);
        }
        return 1;
    }
    return 0;
}
Esempio n. 29
0
tp_obj zzpy__pyDFMenu_set(tp_vm *vm)
{
  tp_obj self = tp_getraw(vm);
  IScriptEngine *engine = (IScriptEngine*)tp_get(vm, vm->builtins, tp_string("userdata")).data.val;
  IEditor *editor = engine->editor();
  TinyParams pm(vm);
  ADM_scriptDFMenuHelper *me = (ADM_scriptDFMenuHelper *)pm.asThis(&self, ADM_PYID_DF_INTEGER);
  char const *key = pm.asString();
  if (!strcmp(key, "index"))
  {
     if(!me) pm.raise("pyDFMenu:No this!");
     int val = pm.asInt();
     me->setIndex(val);
     return tp_None;
  }
  return tp_None;
}
Esempio n. 30
0
tp_obj tp_add(TP,tp_obj a, tp_obj b) {
    if (a.type == TP_NUMBER && a.type == b.type) {
        return tp_number(a.number.val+b.number.val);
    } else if (a.type == TP_STRING && a.type == b.type) {
        int al = a.string.len, bl = b.string.len;
        tp_obj r = tp_string_t(tp,al+bl);
        char *s = r.string.info->s;
        memcpy(s,a.string.val,al); memcpy(s+al,b.string.val,bl);
        return tp_track(tp,r);
    } else if (a.type == TP_LIST && a.type == b.type) {
        tp_obj r;
        tp_params_v(tp,1,a);
        r = tp_copy(tp);
        tp_params_v(tp,2,r,b);
        tp_extend(tp);
        return r;
    }
    tp_raise(tp_None,tp_string("(tp_add) TypeError: ?"));
}