1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

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

This commit is contained in:
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') // println('breaking. "$v.name" v.scope_level=$v.scope_level')
break 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_') { if v.typ.starts_with('array_') {
p.genln('v_array_free($v.name); // close_scope free') 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) opt_type := typ.name.right(7)
p.error('unhandled option type: $opt_type?') p.error('unhandled option type: $opt_type?')
} }
println('error in dot():') //println('error in dot():')
println('fields:') //println('fields:')
for field in typ.fields { //for field in typ.fields {
println(field.name) //println(field.name)
} //}
println('methods:') //println('methods:')
for field in typ.methods { //for field in typ.methods {
println(field.name) //println(field.name)
} //}
println('str_typ=="$str_typ"') //println('str_typ=="$str_typ"')
p.error('type `$typ.name` has no field or method `$field_name`') p.error('type `$typ.name` has no field or method `$field_name`')
} }
mut dot := '.' mut dot := '.'

View File

@ -1,6 +1,19 @@
fn test_mut() { fn foo(a mut []int) {
a := 1 a[0] = 7
mut b := &a a << 4
*b = 10 }
println(a)
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() { fn main() {
println('Running vweb test on http://localhost:$Port')
vweb.run<App>(Port) vweb.run<App>(Port)
} }
@ -27,3 +26,4 @@ pub fn (app mut App) json_endpoint() {
pub fn (app mut App) index() { pub fn (app mut App) index() {
app.vweb.text('hello world') app.vweb.text('hello world')
} }

View File

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

View File

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

View File

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

View File

@ -12,6 +12,7 @@ import os
import glfw import glfw
struct Vec2 { struct Vec2 {
pub:
x int x int
y int y int
} }
@ -24,10 +25,6 @@ import const (
GL_INT GL_INT
) )
const (
DEFAULT_FONT_SIZE = 12
)
pub fn vec2(x, y int) Vec2 { pub fn vec2(x, y int) Vec2 {
res := Vec2 { res := Vec2 {
x: x, x: x,
@ -42,11 +39,9 @@ pub fn init() {
gl.init_glad() gl.init_glad()
} }
struct Face {
cobj voidptr
}
struct Cfg { struct Cfg {
pub:
width int width int
height int height int
use_ortho bool use_ortho bool
@ -95,32 +90,6 @@ pub fn new_context(cfg Cfg) *GG {
shader.use() shader.use()
if cfg.use_ortho { if cfg.use_ortho {
projection := glm.ortho(0, cfg.width, cfg.height, 0) 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) shader.set_mat4('projection', projection)
} }
else { else {
@ -342,6 +311,10 @@ fn (c GG) fill_color(color gx.Color) {
fn (c GG) fill() { 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) { 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[14], 0.000000)
assert cmp(projection.data[15], 1.000000) assert cmp(projection.data[15], 1.000000)
// f := gg.ortho(1,2,3,4) // 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() { fn test_rotate() {

View File

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

View File

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

View File

@ -31,7 +31,7 @@ pub fn (b mut Builder) writeln(s string) {
} }
pub fn (b Builder) str() 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) { pub fn (b Builder) cut(n int) {