http: fix response headers and status code; clean up ft and gg

pull/1484/head
Alexander Medvednikov 2019-08-05 10:42:58 +02:00
parent ff730b4eb0
commit 7a7b5040e2
11 changed files with 131 additions and 96 deletions

View File

@ -1059,7 +1059,7 @@ fn (p mut Parser) close_scope() {
// println('breaking. "$v.name" v.scope_level=$v.scope_level')
break
}
if !p.building_v && !v.is_mut && v.is_alloc {
if false && !p.building_v && !v.is_mut && v.is_alloc {
if v.typ.starts_with('array_') {
p.genln('v_array_free($v.name); // close_scope free')
}
@ -1729,16 +1729,16 @@ fn (p mut Parser) dot(str_typ string, method_ph int) string {
opt_type := typ.name.right(7)
p.error('unhandled option type: $opt_type?')
}
println('error in dot():')
println('fields:')
for field in typ.fields {
println(field.name)
}
println('methods:')
for field in typ.methods {
println(field.name)
}
println('str_typ=="$str_typ"')
//println('error in dot():')
//println('fields:')
//for field in typ.fields {
//println(field.name)
//}
//println('methods:')
//for field in typ.methods {
//println(field.name)
//}
//println('str_typ=="$str_typ"')
p.error('type `$typ.name` has no field or method `$field_name`')
}
mut dot := '.'

View File

@ -1,6 +1,19 @@
fn test_mut() {
a := 1
mut b := &a
*b = 10
println(a)
fn foo(a mut []int) {
a[0] = 7
a << 4
}
fn test_mut() {
mut a := [1,2,3]
foo(mut a)
assert a.len == 4
assert a[0] == 7
assert a[3] == 4
n := 1
mut b := &n
*b = 10
//mut b := mut a
//b = 10
}

View File

@ -12,7 +12,6 @@ pub mut:
}
fn main() {
println('Running vweb test on http://localhost:$Port')
vweb.run<App>(Port)
}
@ -27,3 +26,4 @@ pub fn (app mut App) json_endpoint() {
pub fn (app mut App) index() {
app.vweb.text('hello world')
}

View File

@ -248,7 +248,6 @@ pub fn (m map_string) str() string {
if m.size == 0 {
return '{}'
}
// TODO use bytes buffer
mut sb := strings.new_builder(50)
sb.writeln('{')
for key, val in m {

View File

@ -5,8 +5,8 @@
module builtin
struct string {
mut:
hash_cache int
//mut:
//hash_cache int
pub:
str byteptr
len int
@ -864,7 +864,8 @@ pub fn (c byte) is_white() bool {
pub fn (s string) hash() int {
mut h := s.hash_cache
//mut h := s.hash_cache
mut h := 0
if h == 0 && s.len > 0 {
for c in s {
h = h * 31 + int(c)

View File

@ -4,13 +4,21 @@
module ft
import stbi
import glm
import gl
import (
os
gx
gg
stbi
glm
gl
)
#flag -I @VROOT/thirdparty/freetype
#flag darwin -I/usr/local/Cellar/freetype/2.10.0/include/freetype2
#flag -lfreetype
#flag @VROOT/thirdparty/freetype/libfreetype.a
//#flag -I @VROOT/thirdparty/freetype
//#flag @VROOT/thirdparty/freetype/libfreetype.a
#flag darwin -lpng -lbz2 -lz
@ -21,32 +29,54 @@ import gl
#include "ft2build.h"
#include FT_FREETYPE_H
const (
DEFAULT_FONT_SIZE = 12
)
struct Character {
texture_id u32
size gg.Vec2
bearing gg.Vec2
advance u32
}
struct Face {
cobj voidptr
}
struct GG {
shader gl.Shader
// use_ortho bool
width int
height int
VAO u32
vao u32
rect_vao u32
rect_vbo u32
line_vao u32
line_vbo u32
VBO u32
chars []gg.Character
vbo u32
chars []Character
utf_runes []string
utf_chars []gg.Character
utf_chars []Character
text_ctx *GG
face Face
scale int // retina = 2 , normal = 1
}
struct Character {
texture_id u32
size Vec2
bearing Vec2
advance u32
struct Cfg {
width int
height int
use_ortho bool
retina bool
font_size int
create_window bool
window_user_ptr voidptr
window_title string
always_on_top bool
}
// jfn ft_load_char(face FT_Face, code FT_ULong) Character {
// fn ft_load_char(_face voidptr, _code voidptr) Character {
fn ft_load_char(_face Face, code i64) Character {
@ -86,7 +116,7 @@ fn ft_load_char(_face Face, code i64) Character {
return ch
}
pub fn new_context_text(cfg Cfg, scale int) *GG {
pub fn new_context(cfg gg.Cfg, scale int) *GG {
// Can only have text in ortho mode
if !cfg.use_ortho {
return &GG{text_ctx: 0}
@ -106,7 +136,7 @@ pub fn new_context_text(cfg Cfg, scale int) *GG {
*/
// gl.enable(GL_CULL_FACE) // TODO NEED CULL?
gl.enable(GL_BLEND)
return &GG{}
//return &GG{}
// return &GG{}
# glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
shader := gl.new_shader('text')
@ -124,10 +154,10 @@ return &GG{}
// face := FT_Face{}
mut font_path := 'RobotoMono-Regular.ttf'
if !os.file_exists(font_path) {
exePath := os.getexepath()
exeDir := os.basedir(exePath)
println('Trying to load from $exeDir')
font_path = '${exeDir}/RobotoMono-Regular.ttf'
exe_path := os.executable()
exe_dir := os.basedir(exe_path)
println('Trying to load from $exe_dir')
font_path = '$exe_dir/RobotoMono-Regular.ttf'
}
if !os.file_exists(font_path) {
println('failed to load RobotoMono-Regular.ttf')
@ -147,7 +177,7 @@ return &GG{}
# glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// Gen texture
// Load first 128 characters of ASCII set
mut chars := []gg.Character{}
mut chars := []Character{}
f := Face {
cobj: 0
}
@ -175,11 +205,11 @@ return &GG{}
// # ch = gg__ft_load_char(f, 128169);
// chars.push(ch)
// Configure VAO
VAO := gl.gen_vertex_array()
println('new gg text context VAO=$VAO')
VBO := gl.gen_buffer()
gl.bind_vao(VAO)
gl.bind_buffer(GL_ARRAY_BUFFER, VBO)
vao := gl.gen_vertex_array()
println('new gg text context vao=$vao')
vbo := gl.gen_buffer()
gl.bind_vao(vao)
gl.bind_buffer(GL_ARRAY_BUFFER, vbo)
// # glBufferData(GL_ARRAY_BUFFER, sizeof(GLf32) * 6 * 4, NULL, GL_DYNAMIC_DRAW);
gl.enable_vertex_attrib_array(0)
gl.vertex_attrib_pointer(0, 4, GL_FLOAT, false, 4, 0)
@ -191,8 +221,8 @@ return &GG{}
width: width,
height: height,
scale: scale
VAO: VAO,
VBO: VBO,
vao: vao,
vbo: vbo,
chars: chars,
face: f
text_ctx: 0
@ -271,7 +301,7 @@ fn (ctx &GG) _draw_text(_x, _y int, utext ustring, cfg gx.TextCfg) {
ctx.shader.use()
ctx.shader.set_color('textColor', color)
# glActiveTexture(GL_TEXTURE0);
gl.bind_vao(ctx.VAO)
gl.bind_vao(ctx.vao)
// Iterate through all characters
// utext := text.ustring()
for i := 0; i < utext.len; i++ {
@ -323,7 +353,7 @@ fn (ctx &GG) _draw_text(_x, _y int, utext ustring, cfg gx.TextCfg) {
// t1 := glfw.get_time()
# glBindTexture(GL_TEXTURE_2D, ch.texture_id);
// Update content of VBO memory
gl.bind_buffer(GL_ARRAY_BUFFER, ctx.VBO)
gl.bind_buffer(GL_ARRAY_BUFFER, ctx.vbo)
// t2 := glfw.get_time()
// # glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices); // Be sure to use glBufferSubData and not glBufferData
# glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_DYNAMIC_DRAW);

View File

@ -12,6 +12,7 @@ import os
import glfw
struct Vec2 {
pub:
x int
y int
}
@ -24,10 +25,6 @@ import const (
GL_INT
)
const (
DEFAULT_FONT_SIZE = 12
)
pub fn vec2(x, y int) Vec2 {
res := Vec2 {
x: x,
@ -42,11 +39,9 @@ pub fn init() {
gl.init_glad()
}
struct Face {
cobj voidptr
}
struct Cfg {
pub:
width int
height int
use_ortho bool
@ -95,32 +90,6 @@ pub fn new_context(cfg Cfg) *GG {
shader.use()
if cfg.use_ortho {
projection := glm.ortho(0, cfg.width, cfg.height, 0)
/*
// for debugging broken tetris in gg.o
# projection.data[0]=0.010000;
# projection.data[1]=0.000000;
# projection.data[2]=0.000000;
# projection.data[3]=0.000000;
# projection.data[4]=0.000000;
# projection.data[5]=-0.005000;
# projection.data[6]=0.000000;
# projection.data[7]=0.000000;
# projection.data[8]=0.000000;
# projection.data[9]=0.000000;
# projection.data[10]=1.000000;
# projection.data[11]=0.000000;
# projection.data[12]=-1.000000;
# projection.data[13]=1.000000;
# projection.data[14]=0.000000;
# projection.data[15]=1.000000;
*/
// projection_new := ortho(0, width, height, 0)
// println('\nORTHO OLD=')
//# for (int i=0;i<16;i++) printf("%d=%f ",i, projection.data[i]);
// println('\n\n!ORTHO NEW=')
// # for (int i=0;i<16;i++) printf("%d=%f ",i, projection_new[i]);
// println('\n\n')
//println('setting o')
shader.set_mat4('projection', projection)
}
else {
@ -342,6 +311,10 @@ fn (c GG) fill_color(color gx.Color) {
fn (c GG) fill() {
}
pub fn (ctx &GG) draw_text(_x, _y int, text string, cfg gx.TextCfg) {
//pub fn (c &GG) draw_text(x, y int) {
}
fn (c GG) move_to(x, y int) {
}

View File

@ -30,6 +30,26 @@ fn test_ortho() {
assert cmp(projection.data[14], 0.000000)
assert cmp(projection.data[15], 1.000000)
// f := gg.ortho(1,2,3,4)
/*
// for debugging broken tetris in gg.o
# projection.data[0]=0.010000;
# projection.data[1]=0.000000;
# projection.data[2]=0.000000;
# projection.data[3]=0.000000;
# projection.data[4]=0.000000;
# projection.data[5]=-0.005000;
# projection.data[6]=0.000000;
# projection.data[7]=0.000000;
# projection.data[8]=0.000000;
# projection.data[9]=0.000000;
# projection.data[10]=1.000000;
# projection.data[11]=0.000000;
# projection.data[12]=-1.000000;
# projection.data[13]=1.000000;
# projection.data[14]=0.000000;
# projection.data[15]=1.000000;
*/
}
fn test_rotate() {

View File

@ -50,7 +50,8 @@ fn write_fn(contents byteptr, size, nmemb int, _mem *MemoryStruct) int {
mut mem := _mem
// # printf("size =%d nmemb=%d contents=%s\n", size, nmemb, contents);
realsize := size * nmemb// TODO size_t ?
if !isnil(mem.ws_func) {
/*
if false && !isnil(mem.ws_func) {
//C.printf('\n\nhttp_mac.m: GOT WS FUNC. size=%d\n', realsize)
// Skip negative and 0 junk chars in the WS string
mut start := 0
@ -62,12 +63,13 @@ fn write_fn(contents byteptr, size, nmemb int, _mem *MemoryStruct) int {
}
}
contents += start + 1
// printf("GOOD CONTEnTS=%s\n", contents);
C.printf('GOOD CONTEnTS=%s\n', contents)
s := string(contents)
f := mem.ws_func
f(s, mem.user_ptr)
//# mem->ws_func(s, mem->user_ptr);
}
*/
mut c := string(contents)
c = c.trim_space()
// Need to clone because libcurl reuses this memory
@ -169,22 +171,18 @@ pub fn (req &Request) do() Response {
continue
}
key := h.left(pos)
val := h.right(pos + 1)
// println('"$key" *** "$val"')
val := h.right(pos + 2)
//println('"$h" "$key" *** "$val"')
// val2 := val.trim_space()
// println('val2="$val2"')
headers[key] = val// val.trim_space()
}
// println('done')
// j.println(resp.status_code)
// println('body=')
// j.println(resp.body)
// j.println('headers=')
// j.println(hchunk.strings)
C.curl_easy_cleanup(curl)
//println('end of req.do() url="$req.url"')
return Response {
text: body
status_code: status_code
headers: headers
}
}

View File

@ -7,3 +7,4 @@ fn test_escape_unescape() {
unescaped := http.unescape_url(escaped)
assert unescaped == original
}

View File

@ -31,7 +31,7 @@ pub fn (b mut Builder) writeln(s string) {
}
pub fn (b Builder) str() string {
return tos(b.buf.data, b.len)
return string(b.buf, b.len)
}
pub fn (b Builder) cut(n int) {