{{Angular}}, {{Ember}} or {Drop}?

Okay, this post is actually not about comparing these three frameworks. But to introduce my DropJS.

Though actually I can’t compare DropJS to the others, even if I want to… Because it just can’t. But it exists for reasons. For example: why not {single braces} instead of {{double braces}}; why lacking of a gentle escaping notation; why conventions, especially why conventions on strings and letter capitalization…

I had some thoughts on MVVM framework years ago and actually tried to implement DropJS years ago. Sadly I didn’t make it. One of the reasons is that the template style I used was not very friendly to parse (though I do remember I had written a parser successfully, then maybe it was because other reasons). As for Drop, it’s clear and simple. To make everything easier, I come up with an idea called “decorators”. It’s somewhat like the concept of “directives” in Angular based on my understanding (though not very good understanding), but written in a way of {handlebars}, single-brace {handlebars}. ;)

Some code:

<!-- AngularJS -->
<input type="text" ng-model="name" />
<h1>Hello {{name}}!</h1>

<!-- EmberJS -->
{{input type="text" value=name}}
<h1>Hello {{name}}!</h1>

<!-- DropJS -->
{%var name}
{%bind-value name}
<input type="text" />
<h1>Hello {name}!</h1>

I have to say that the code written in Drop is a little bit more than Angular and Ember. But actually {%var name} can be removed if {name} exists in the model. Or you can just write your own {%bind-value} decorator with the ability to define variables under the scope. Because even {%var} is just a normal decorator.

You may want to check out the source code of these decorators here lib/decorators.ts. Yup, Drop is written in TypeScript.

GitHub https://github.com/vilic/drop
Demo https://rawgit.com/vilic/drop/master/demo/index.html

Original link of this archive: http://vilic.info/blog/archives/1150
本文的原始链接: http://vilic.info/blog/archives/1150

Windows Phone C++ Load System Dll Using LoadLibraryExW

I was trying to write a Windows Phone 8 app using undocumented API. @imbushuo told me that there was an example on xda-developer using complete Windows API on a store app, and it works great.

And then I found a better implementation from this post in Chinese (The approach is actually mentioned in a comment of the post in xda-developer). Though it’s missing some part.

Here is the core code of the implementation mentioned above:

#include "pch.h"

typedef struct _CLIENT_ID {
	PVOID UniqueProcess;
	PVOID UniqueThread;


typedef struct _MODULE_LIST_ENTRY {
	struct  _MODULE_LIST_ENTRY* Flink;
	struct  _MODULE_LIST_ENTRY* Blink;
	DWORD* baseAddress;


typedef struct _PEB_LDR_DATA {
	//    BYTE fill[0x1c]; x86
	ULONG Length;
	BOOLEAN Initialized;
	PVOID SsHandle;
	LIST_ENTRY InLoadOrderModuleList;
	LIST_ENTRY InMemoryOrderModuleList;
	MODULE_LIST_ENTRY* initModuleList;


typedef struct _PEB {
	//    BYTE fill[0x0c]; x86
	BYTE Reserved1[2];
	BYTE BeingDebugged;
	BYTE Reserved2[1];
	PVOID Reserved3[2];

} PEB;

typedef struct _TEB {
	//BYTE fill[0x30]; x86
	NT_TIB nt_tib;
	PVOID EnvironmentPointer;
	PVOID ActiveRpcHandle;
	PVOID ThreadLocalStoragePointer;
	PEB* currentPEB;
} TEB;

typedef HMODULE(*LoadLibraryEx)(
	LPCTSTR lpLibFileName,
	HANDLE hFile,
	DWORD dwFlags

int __cdecl main(::Platform::Array<::Platform::String^>^ args) {
	TEB* teb = NtCurrentTeb();
	HMODULE kernel = (HMODULE) teb->currentPEB->ldr->initModuleList->Flink->baseAddress;
	LoadLibraryEx LoadLibraryExW = (LoadLibraryEx) GetProcAddress(kernel, "LoadLibraryExW");

	// do your jobs...

Original link of this archive: http://vilic.info/blog/archives/1138
本文的原始链接: http://vilic.info/blog/archives/1138

TypeScript or Pure JavaScript?

I was really excited when I got to know that Microsoft had released a new language called TypeScript.

Type, which means better intellisense and type safety that serve the purpose of its existence — scalable.

Why is this important?

  1. Type safety.
    Hate bugs? There’s a joke we talk a lot, which is also a truth: “The weirder the bug is, the stupider mistake we might have made.” And the compiler would now worry about some of these things for us, like typo or forgetting to change a property after copying some code.
  2. Tooling.
    When a project grows, the API might become too complex for the developer himself to remember. Visual Studio provides great experience on JavaScript intellisense by actually running the code in background. But on the one hand, it would usually requires a special rewritten-version of intellisense file. On the other hand, it would become very slow when code lines add up. (I wrote a framework called VEJIS which picks up the powerful intellisense feature of Visual Studio, provides support for classes, interfaces, delegates, etc. But it’s a runtime framework and there’s no type safety. Checkout http://vilic.github.io/vejis/)
    TypeScript makes code navigation much easier and most of the time, we won’t even need to navigate because the information we need is there with intellisense.

Searching articles related to TypeScript would result in many that are comparing it to CoffeeScript and Dart. It’s really weird because some (maybe most) of these articles treats TypeScript the same sort of thing as CoffeeScript simply because both of them compile to JavaScript. And I have even seen an opinion saying: “If you want to build a big application I’d recommend going with CoffeeScript as you end up writing less code.”

WHAT? Maybe that buddy haven’t really met something big enough.

CoffeeScript won’t do things better for people like me, who have been writing JavaScript since day one. People like to talk about the “good part” when it comes to CoffeeScript, but… does it really take more effort for one person who starts with JavaScript to remember how to avoid the bad part than learning a syntactically brand new language?

And actually I have never given the “good or bad part” thing much credit.

So back to the topic, TypeScript or pure JavaScript? Ignoring things other than the languages themselves, I go with TypeScript without hesitate. Because it’s a superset of JavaScript, and it provides much more useful features for larger-scale applications. And, the key point, I write relatively large-scale web applications. However, to use TypeScript in productive environment, there would be more to think about.

  1. Poor IDE and editor plugin support.
    There have been several IDEs with TypeScript support integrated. Visual Studio, of course, would be one of them. But the experience coding using Visual Studio can’t even beat the experience on TypeScript Playground. This really bothers me a lot. No automatic-quote/bracket completion, no snippets, strange indent behavior, etc.
    I haven’t tried TypeScript in WebStorm, maybe it would have done better job. Also ReSharper for Visual Studio may improve the experience according to some comments I’ve seen.
    There are also some plugins for Sublime Text, but… you know.
  2. Poor NPM package and cross-project referencing support.
    Actually TypeScript is capable generating definition files so that it should have been friendly to these things. But the reality is not that awesome…
    I have proposed a convention on distributing TypeScript-written NPM package, and hoping it would make things better.

If finally you choose TypeScript, here’s some information and techniques that might help.

  1. Definitions of popular JavaScript libraries.
    I don’t have any crush on open source, but here open source does it right. So far most of the declarations I need for specific JavaScript libraries can be found in DefinitelyTyped, and it’s becoming of better quality and of larger covering.
    You may install the declarations though NuGet on Visual Studio, to make things work after installation, try to refresh the solution tree.
  2. Temporary “best” practice for cross-project referencing in Visual Studio.
    In project properties page TypeScript Build tab, check “Combine JavaScript output into file” (and specify a file name) and “Generate declaration files”.
    Create a “.d.ts” file in parent project and add the declaration file generated by sub project to it as a reference.
    Visual Studio would compile all “.ts” files included in the project into the JavaScript file specified, so make sure other test files are excluded from the project or you may want to create another project for samples and tests.
    When it comes to NPM packages, I haven’t figure out an acceptable way. But following that convention I mentioned earlier in this post, I may try to write an extension for Visual Studio which would pick up declaration files automatically, wrap it up with ambient module name and add the modified declaration file into the project that’s using that module. It would also be useful even if we are using these packages ourselves without publishing it to npmjs.org only. (BTW, symlink would save you hours if you didn’t know.)

I have now a symptom trying to make everything typed, it slows me down starting up coding a project, but speeds my lines up once the skeleton completes. Hope you enjoy writing in TypeScript if you need it.

(BTW it would be great if TypeScript add support for await/async based on Promise.)

Original link of this archive: http://vilic.info/blog/archives/1136
本文的原始链接: http://vilic.info/blog/archives/1136

A simple URL shorter for NodeJS

It’s called Biu, because in Chinese this Pinyin (something like phonetic symbols) pronounces like “be-yo”, the sound that old-fashioned laser gun would make in films.

I decide not to use database but instead use a single text file, because for a private URL shorter, the links number would hardly be big. And it will read all the links into memory once starts.

Considering its application scene, I think it’s rational to make things, including setup, simpler.

Demo http://biu.link
Github http://biu.link/git (https://github.com/vilic/biu)

Original link of this archive: http://vilic.info/blog/archives/1129
本文的原始链接: http://vilic.info/blog/archives/1129