Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undeclared type longer than 63 characters #19783

Closed
MCausc78 opened this issue Nov 6, 2023 · 2 comments
Closed

Undeclared type longer than 63 characters #19783

MCausc78 opened this issue Nov 6, 2023 · 2 comments
Labels
Bug This tag is applied to issues which reports bugs. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.

Comments

@MCausc78
Copy link
Contributor

MCausc78 commented Nov 6, 2023

Describe the bug

since TCC does not support symbols longer than 63 characters, V strips out symbol name to 63 characters

Reproduction Steps

module main

//import coroutines
import net.websocket
import x.json2
import time

const (
	undefined_sequence = 0x7fffffff
)

pub struct GatewayMessage {
pub:
	opcode int
	data json2.Any
	seq int = undefined_sequence
	event string
}


fn decode_message(payload json2.Any) !GatewayMessage {
	if payload !is map[string]json2.Any {
		return error('expected object')
	}
	mut opcode := 0
	m := payload.as_map()
	if op := m['op'] {
		match op {
			int { opcode = op }
			else { return error('opcode is not int') }
		}
	} else {
		return error('no op')
	}
	mut seq := undefined_sequence
	if s := m['s'] {
		match s {
			int { seq = s }
			else { return error('seq is not int') }
		}
	}
	mut event := ''
	if t := m['t'] {
		match t {
			string { event = t }
			else { return error('event is not string') }
		}
	}
	mut data := json2.Any(json2.Null{})
	if d := m['d'] {
		data = d
	}
	return GatewayMessage{opcode: opcode, data: data, seq: seq, event: event}
}

fn encode_message(message GatewayMessage) !json2.Any {
	return {
		'op': json2.Any(message.opcode),
		'd': message.data
	}
}

fn decode_websocket_message(message websocket.Message) !GatewayMessage {
	return decode_message(json2.decode[json2.Any](message.payload.str())!)!
}

fn gateway_recv_message(mut client &websocket.Client) !GatewayMessage {
	return decode_websocket_message(client.read_next_message()!)!
}

fn gateway_send_message(mut client &websocket.Client, message GatewayMessage) ! {
	client.write(encode_message(message)!.json_str().bytes(), websocket.OPCode.text_frame)!
}


pub struct Client {
pub:
	token string
	intents int
mut:
	gateway &websocket.Client = unsafe { nil }
	ready bool
	sequence int = undefined_sequence
}

fn (mut c Client) recv() !GatewayMessage {
	return gateway_recv_message(mut c.gateway)!
}

fn (mut c Client) send(message GatewayMessage) ! {
	gateway_send_message(mut c.gateway, message)!
}

fn (mut c Client) init() ! {
	mut gateway := websocket.new_client('wss://gateway.discord.gg?v=10&encoding=json')!
	c.gateway = gateway
	c.ready = false
	gateway.on_message_ref(fn (mut _ websocket.Client, m &websocket.Message, r voidptr) ! {
		mut client := unsafe { &Client(r) }
		message := decode_websocket_message(m)!
		if !client.ready {
			if message.opcode != 10 {
				return error('message is not HELLO')
			}
			client.send(GatewayMessage{
				opcode: 2,
				data: json2.Any({
					'token': json2.Any(client.token),
					'intents': client.intents,
					'properties': {
						'os': json2.Any('windows'),
						'browser': 'v',
						'device': 'Discord Android'
					}
				})
			})!
			spawn fn(mut client &Client, heartbeat_interval time.Duration) ! {
				for client.ready {
					time.sleep(heartbeat_interval)
					client.send(GatewayMessage{
						opcode: 1,
						data: json2.Any(1)
					})!
				}
			}(mut client, time.millisecond * i64(message.data.as_map()['heartbeat_interval']! as int))
			client.ready = true
			return 
		}
		if message.opcode == 0 {
			println('dispatch!')
			println('event: ${message.event}')
			println('data: ${message.data}')
		}
	}, &c)


}

fn (mut c Client) run() ! {
	c.gateway.connect()!
	c.gateway.listen()!
}


fn main() {
	mut c := Client{token: 'Bot ...', intents: 33280}
	c.init() or { panic(err) }
	c.run() or { panic(err) }
	/*mut client := websocket.new_client('wss://gateway.discord.gg?v=10&encoding=json') or { panic(err) }

	client.on_open(fn (mut c websocket.Client) ! {
		message := gateway_recv_message(mut c)!
		if message.opcode != 10 {
			return error('message is not HELLO')
		}
		gateway_send_message(mut c, GatewayMessage{
			opcode: 2,
		})!
		spawn fn(mut client websocket.Client, heartbeat_interval time.Duration) ! {
			coroutines.sleep(heartbeat_interval)
			gateway_send_message(mut client, GatewayMessage{opcode: 1, data: json2.Any(1)})!
		}(mut c, time.millisecond * i64(message.data.as_map()['heartbeat_interval']! as int))
	})
	client.connect() or { panic(err) }
	client.listen() or { panic(err) }*/
	
}

Expected Behavior

Normal compile without errors

Current Behavior

> v run .
==================
D:/Games/Proekti/V/v/thirdparty/vschannel/vschannel.c:300: warning: assignment from incompatible pointer type
C:/Users/mclr/AppData/Local/Temp/v_0/interactions.9499441320864990899.tmp.c:8386: error: 'thread_arg_anon_fn_f860512d614a01ac_mut_main__client_time__dura' undeclared
...
==================
(Use `v -cg` to print the entire error message)

builder error:
==================
C error. This should never happen.

This is a compiler bug, please report it using `v bug file.v`.

https://github.com/vlang/v/issues/new/choose

You can also use #help on Discord: https://discord.gg/vlang

Possible Solution

If using TCC and symbol is longer than 63 characters, rename symbol to vc_t<sequence number>_<hash of symbol in hex>

Additional Information/Context

I'm trying to make simple discord bot using interactions

V version

V 0.4.2 140c838

Environment details (OS name and version, etc.)

full version: V 0.4.2 4e6186e.140c838
OS: windows, Microsoft Windows 11 Pro v22000 64-bit
Processor: 12 cpus, 64bit, little endian,

getwd: D:\Games\Proekti\V\interactions
vexe: D:\Games\Proekti\V\v\v.exe
vexe mtime: 2023-11-06 10:38:17

vroot: OK, value: D:\Games\Proekti\V\v
VMODULES: OK, value: C:\Users\mclr.vmodules
VTMP: OK, value: C:\Users\mclr\AppData\Local\Temp\v_0

Git version: git version 2.37.0.windows.1
Git vroot status: weekly.2023.44-63-g140c838f
.git/config present: true

CC version: Error: 'cc' is not recognized as an internal or external command,
operable program or batch file.

thirdparty/tcc status: thirdparty-windows-amd64 e90c2620

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

@MCausc78 MCausc78 added the Bug This tag is applied to issues which reports bugs. label Nov 6, 2023
@medvednikov
Copy link
Member

A great find! Thanks for the detailed report.

@ArtemkaKun ArtemkaKun added Unit: Type System Bugs/feature requests, that are related to the V types system. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. and removed Unit: Type System Bugs/feature requests, that are related to the V types system. labels Nov 6, 2023
@MCausc78
Copy link
Contributor Author

MCausc78 commented Jan 3, 2024

already does not reproduces, it shows another error:

==================
D:/Games/Proekti/V/v/thirdparty/vschannel/vschannel.c:300: warning: assignment from incompatible pointer type
C:/Users/mclr/AppData/Local/Temp/v_0/test.17343530584298790918.tmp.c:8534: error: ')' expected (got "_t9")
...
==================
(Use `v -cg` to print the entire error message)

builder error:
==================
C error. This should never happen.

This is a compiler bug, please report it using `v bug file.v`.

https://github.com/vlang/v/issues/new/choose

You can also use #help on Discord: https://discord.gg/vlang

@MCausc78 MCausc78 closed this as completed Jan 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.
Projects
None yet
Development

No branches or pull requests

3 participants