summaryrefslogtreecommitdiffstatshomepage
path: root/unix
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-01-21 21:40:13 +0000
committerDamien George <damien.p.george@gmail.com>2014-01-21 21:40:13 +0000
commit55baff4c9bcbc001cbb8972c289ebfa356d4665b (patch)
treebd086f9ddf8c5f2db9642ee04fc382064ebd2029 /unix
parent91d457a27752fa125e9c6107bf51c918e021dc95 (diff)
downloadmicropython-55baff4c9bcbc001cbb8972c289ebfa356d4665b.tar.gz
micropython-55baff4c9bcbc001cbb8972c289ebfa356d4665b.zip
Revamp qstrs: they now include length and hash.
Can now have null bytes in strings. Can define ROM qstrs per port using qstrdefsport.h
Diffstat (limited to 'unix')
-rw-r--r--unix/.gitignore2
-rw-r--r--unix/Makefile7
-rw-r--r--unix/file.c2
-rw-r--r--unix/main.c17
-rw-r--r--unix/qstrdefsport.h13
-rw-r--r--unix/socket.c22
6 files changed, 40 insertions, 23 deletions
diff --git a/unix/.gitignore b/unix/.gitignore
index a90e4976e6..390bdd300e 100644
--- a/unix/.gitignore
+++ b/unix/.gitignore
@@ -1,3 +1,3 @@
build
-py
+micropython
*.py
diff --git a/unix/Makefile b/unix/Makefile
index 454ded79af..7a4ce3e161 100644
--- a/unix/Makefile
+++ b/unix/Makefile
@@ -2,6 +2,9 @@
PROG = micropython
all: $(PROG)
+# qstr definitions (must come before including py.mk)
+QSTR_DEFS = qstrdefsport.h
+
# include py core make definitions
include ../py/py.mk
@@ -27,7 +30,7 @@ SRC_C = \
file.c \
socket.c \
-OBJ = $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) $(PY_O)
+OBJ = $(PY_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
LIB = -lreadline
# the following is needed for BSD
#LIB += -ltermcap
@@ -40,7 +43,7 @@ ifndef DEBUG
endif
$(Q)size $(PROG)
-$(BUILD)/%.o: %.c
+$(BUILD)/%.o: %.c $(QSTR_DEFS)
$(ECHO) "CC $<"
$(Q)$(CC) $(CFLAGS) -c -o $@ $<
diff --git a/unix/file.c b/unix/file.c
index af75944711..72c56ca100 100644
--- a/unix/file.c
+++ b/unix/file.c
@@ -6,7 +6,7 @@
#include "nlr.h"
#include "misc.h"
#include "mpconfig.h"
-#include "mpqstr.h"
+#include "qstr.h"
#include "obj.h"
#include "stream.h"
diff --git a/unix/main.c b/unix/main.c
index 08567c04dc..74e903c2ef 100644
--- a/unix/main.c
+++ b/unix/main.c
@@ -6,6 +6,7 @@
#include "nlr.h"
#include "misc.h"
#include "mpconfig.h"
+#include "qstr.h"
#include "lexer.h"
#include "lexerunix.h"
#include "parse.h"
@@ -216,12 +217,12 @@ int main(int argc, char **argv) {
qstr_init();
rt_init();
- mp_obj_t m_sys = mp_obj_new_module(qstr_from_str_static("sys"));
+ mp_obj_t m_sys = mp_obj_new_module(MP_QSTR_sys);
mp_obj_t py_argv = mp_obj_new_list(0, NULL);
- rt_store_attr(m_sys, qstr_from_str_static("argv"), py_argv);
+ rt_store_attr(m_sys, MP_QSTR_argv, py_argv);
- rt_store_name(qstr_from_str_static("test"), test_obj_new(42));
- rt_store_name(qstr_from_str_static("open"), (mp_obj_t)&mp_builtin_open_obj);
+ rt_store_name(qstr_from_str("test"), test_obj_new(42));
+ rt_store_name(MP_QSTR_open, (mp_obj_t)&mp_builtin_open_obj);
rawsocket_init();
// Here is some example code to create a class and instance of that class.
@@ -232,9 +233,9 @@ int main(int argc, char **argv) {
// test_obj = TestClass()
// test_obj.attr = 42
mp_obj_t test_class_type, test_class_instance;
- test_class_type = mp_obj_new_type(qstr_from_str_static("TestClass"), mp_const_empty_tuple, mp_obj_new_dict(0));
- rt_store_name(qstr_from_str_static("test_obj"), test_class_instance = rt_call_function_0(test_class_type));
- rt_store_attr(test_class_instance, qstr_from_str_static("attr"), mp_obj_new_int(42));
+ test_class_type = mp_obj_new_type(QSTR_FROM_STR_STATIC("TestClass"), mp_const_empty_tuple, mp_obj_new_dict(0));
+ rt_store_name(QSTR_FROM_STR_STATIC("test_obj"), test_class_instance = rt_call_function_0(test_class_type));
+ rt_store_attr(test_class_instance, QSTR_FROM_STR_STATIC("attr"), mp_obj_new_int(42));
/*
printf("bytes:\n");
@@ -259,7 +260,7 @@ int main(int argc, char **argv) {
}
} else {
for (int i = a; i < argc; i++) {
- rt_list_append(py_argv, MP_OBJ_NEW_QSTR(qstr_from_strn_copy(argv[i], strlen(argv[i]))));
+ rt_list_append(py_argv, MP_OBJ_NEW_QSTR(qstr_from_str(argv[i])));
}
do_file(argv[a]);
break;
diff --git a/unix/qstrdefsport.h b/unix/qstrdefsport.h
new file mode 100644
index 0000000000..3178f0a826
--- /dev/null
+++ b/unix/qstrdefsport.h
@@ -0,0 +1,13 @@
+// qstrs specific to this port
+
+Q(sys)
+Q(argv)
+Q(open)
+Q(rawsocket)
+Q(socket)
+Q(sockaddr_in)
+Q(htons)
+Q(inet_aton)
+Q(gethostbyname)
+Q(getaddrinfo)
+Q(rawsocket)
diff --git a/unix/socket.c b/unix/socket.c
index 708cf7af52..e43695c6b6 100644
--- a/unix/socket.c
+++ b/unix/socket.c
@@ -10,7 +10,7 @@
#include "nlr.h"
#include "misc.h"
#include "mpconfig.h"
-#include "mpqstr.h"
+#include "qstr.h"
#include "obj.h"
#include "objtuple.h"
#include "objarray.h"
@@ -249,7 +249,7 @@ static mp_obj_t mod_socket_getaddrinfo(uint n_args, const mp_obj_t *args) {
// "canonname will be a string representing the canonical name of the host
// if AI_CANONNAME is part of the flags argument; else canonname will be empty." ??
if (addr->ai_canonname) {
- t->items[3] = MP_OBJ_NEW_QSTR(qstr_from_strn_copy(addr->ai_canonname, strlen(addr->ai_canonname)));
+ t->items[3] = MP_OBJ_NEW_QSTR(qstr_from_str(addr->ai_canonname));
} else {
t->items[3] = mp_const_none;
}
@@ -262,23 +262,23 @@ static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_socket_getaddrinfo_obj, 2, 6, mod
extern mp_obj_type_t sockaddr_in_type;
-#define STORE_INT_CONST(m, name) rt_store_attr(m, qstr_from_str_static(#name), MP_OBJ_NEW_SMALL_INT(name))
+#define STORE_INT_CONST(m, name) rt_store_attr(m, QSTR_FROM_STR_STATIC(#name), MP_OBJ_NEW_SMALL_INT(name))
void rawsocket_init() {
- mp_obj_t m = mp_obj_new_module(qstr_from_str_static("rawsocket"));
- rt_store_attr(m, qstr_from_str_static("socket"), (mp_obj_t)&rawsocket_type);
+ mp_obj_t m = mp_obj_new_module(MP_QSTR_rawsocket);
+ rt_store_attr(m, MP_QSTR_socket, (mp_obj_t)&rawsocket_type);
#if MICROPY_SOCKET_EXTRA
- rt_store_attr(m, qstr_from_str_static("sockaddr_in"), (mp_obj_t)&sockaddr_in_type);
- rt_store_attr(m, qstr_from_str_static("htons"), (mp_obj_t)&mod_socket_htons_obj);
- rt_store_attr(m, qstr_from_str_static("inet_aton"), (mp_obj_t)&mod_socket_inet_aton_obj);
- rt_store_attr(m, qstr_from_str_static("gethostbyname"), (mp_obj_t)&mod_socket_gethostbyname_obj);
+ rt_store_attr(m, MP_QSTR_sockaddr_in, (mp_obj_t)&sockaddr_in_type);
+ rt_store_attr(m, MP_QSTR_htons, (mp_obj_t)&mod_socket_htons_obj);
+ rt_store_attr(m, MP_QSTR_inet_aton, (mp_obj_t)&mod_socket_inet_aton_obj);
+ rt_store_attr(m, MP_QSTR_gethostbyname, (mp_obj_t)&mod_socket_gethostbyname_obj);
#endif
- rt_store_attr(m, qstr_from_str_static("getaddrinfo"), (mp_obj_t)&mod_socket_getaddrinfo_obj);
+ rt_store_attr(m, MP_QSTR_getaddrinfo, (mp_obj_t)&mod_socket_getaddrinfo_obj);
STORE_INT_CONST(m, AF_UNIX);
STORE_INT_CONST(m, AF_INET);
STORE_INT_CONST(m, AF_INET6);
STORE_INT_CONST(m, SOCK_STREAM);
STORE_INT_CONST(m, SOCK_DGRAM);
STORE_INT_CONST(m, SOCK_RAW);
- rt_store_name(qstr_from_str_static("rawsocket"), m);
+ rt_store_name(MP_QSTR_rawsocket, m);
}